Dav*_*ave 33 python fixtures pytest
py.test在哪里以及如何寻找灯具?我在同一文件夹中的2个文件中有相同的代码.当我删除conftest.py时,找不到运行test_conf.py的cmdopt(也在同一个文件夹中.为什么没有搜索到sonoftest.py?
# content of test_sample.py
def test_answer(cmdopt):
if cmdopt == "type1":
print ("first")
elif cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
文档说
http://pytest.org/latest/fixture.html#fixture-function
- pytest因test_前缀而找到test_ehlo.测试函数需要一个名为smtp的函数参数.通过查找名为smtp的夹具标记函数来发现匹配夹具功能.
- 调用smtp()来创建实例.
- 调用test_ehlo()并在测试函数的最后一行失败.
eca*_*mur 31
默认情况下,py.test将导入conftest.py与python_files模式匹配的所有Python文件test_*.py.如果您有测试夹具,则需要在conftest.py依赖于它的测试文件中包含或从中导入它:
from sonoftest import pytest_addoption, cmdopt
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,并花了很多时间找到一个简单的解决方案,这个例子适用于其他与我有类似情况的人。
import pytest
pytest_plugins = [
"some_package.sonoftest"
]
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
import pytest
@pytest.fixture
def sono_cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
def test_answer1(cmdopt):
if cmdopt == "type1":
print ("first")
elif cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
def test_answer2(sono_cmdopt):
if sono_cmdopt == "type1":
print ("first")
elif sono_cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)
您可以在这里找到类似的示例: https: //github.com/pytest-dev/pytest/issues/3039#issuecomment-464489204 以及其他此处/sf/answers/3831546351/
官方pytest文档的描述:https://docs.pytest.org/en/latest/reference.html? highlight=pytest_plugins#pytest-plugins
请注意,所引用的各个目录
some_package.test_sample"需要有用于__init__.py加载插件的文件pytest
| 归档时间: |
|
| 查看次数: |
21612 次 |
| 最近记录: |