Python py.test如何以及在何处找到夹具
py.test在何处以及如何查找夹具?我在同一文件夹的2个文件中有相同的代码。删除conftest.py时,无法找到运行test_conf.py的cmdopt(也在同一文件夹中)。为什么不搜索sonoftest.pyPython py.test如何以及在何处找到夹具,python,fixtures,pytest,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": pr
# 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
conftest.py的内容
sonoftest.py的内容
医生说
py.test将导入
conftest.py
和所有与Python_文件
模式匹配的Python文件,默认情况下test.*.py
。如果您有测试夹具,则需要从conftest.py
或依赖它的测试文件中包含或导入它:
from sonoftest import pytest_addoption, cmdopt
以下是py.test查找夹具(和测试)的顺序和位置(取自): py.test在工具启动时以以下方式加载插件模块:
-p name
选项,并在实际的命令行解析之前加载指定的插件conftest.py
文件(测试文件及其所有父目录)
conftest.py
子目录中的文件默认情况下不会在
工具启动conftest.py
文件中pytest_plugins变量指定的所有插件我也遇到过同样的问题,花了很多时间找到了一个简单的解决方案,这个例子适用于其他和我有类似情况的人
- conftest.py:
导入pytest
pytest_插件=[
“一些包。sonoftest”
]
def pytest_addoption(解析器):
parser.addoption(“--cmdopt”,action=“store”,default=“type1”,
help=“我的选项:类型1或类型2”)
@pytest.fixture
def cmdopt(请求):
return request.config.getoption(“--cmdopt”)
- 一些_包/sonoftest.py:
导入pytest
@pytest.fixture
def sono_cmdopt(请求):
return request.config.getoption(“--cmdopt”)
- 一些\u包/测试\u sample.py
def测试应答1(cmdopt):
如果cmdopt==“类型1”:
打印(“第一次”)
elif cmdopt==“类型2”:
打印(“第二次”)
断言0以查看打印的内容
def测试应答2(sono cmdopt):
如果sono_cmdopt==“类型1”:
打印(“第一次”)
elif sono_cmdopt==“类型2”:
打印(“第二次”)
断言0以查看打印的内容
您可以在此处找到类似的示例:
还有其他的
官方pytest文档中的描述:
请注意,在
一些包.test\u示例“
需要有\uuuu init\uuuuuu.py
文件,以便pytest
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")
from sonoftest import pytest_addoption, cmdopt