Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py.test如何以及在何处找到夹具_Python_Fixtures_Pytest - Fatal编程技术网

Python py.test如何以及在何处找到夹具

Python 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

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
conftest.py的内容 sonoftest.py的内容 医生说

  • pytest由于test前缀而查找test_ehlo。测试函数需要名为smtp的函数参数。匹配的fixture 函数是通过查找一个名为 smtp
  • 调用smtp()以创建实例
  • 调用了test_ehlo(),但在测试函数的最后一行失败

  • py.test将导入
    conftest.py
    和所有与
    Python_文件
    模式匹配的Python文件,默认情况下
    test.*.py
    。如果您有测试夹具,则需要从
    conftest.py
    或依赖它的测试文件中包含或导入它:

    from sonoftest import pytest_addoption, cmdopt
    

    以下是py.test查找夹具(和测试)的顺序和位置(取自):

    py.test在工具启动时以以下方式加载插件模块:

  • 通过加载所有内置插件

  • 通过加载通过setuptools入口点注册的所有插件

  • 通过预扫描命令行中的
    -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