Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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 pytest fixture位于单独的目录中_Python_Testing_Pytest_Fixtures - Fatal编程技术网

Python pytest fixture位于单独的目录中

Python pytest fixture位于单独的目录中,python,testing,pytest,fixtures,Python,Testing,Pytest,Fixtures,我希望创建一个pytest结构,在这个结构中,我可以将fixture与测试完全分离。这种分离的原因是我想在subversion中将fixtures目录作为一个外部项包括在内,并在多个项目之间共享它 所需结构的树 project | conftest.py | +---fixtures | __init__.py | conftest.py | fixture_cifs.py | fixture_ftp.py | fixture_se

我希望创建一个pytest结构,在这个结构中,我可以将fixture与测试完全分离。这种分离的原因是我想在subversion中将fixtures目录作为一个外部项包括在内,并在多个项目之间共享它

所需结构的树

project
|   conftest.py
|
+---fixtures
|       __init__.py
|       conftest.py
|       fixture_cifs.py
|       fixture_ftp.py
|       fixture_service.py
|
\---tests
    |   test_sometest1.py
    |   test_sometest2.py
    |
    \---configurations
            sometest1.conf
            sometest2.conf
我想在一个单独的文件中实现每个fixture的功能,以避免单个巨大的
conftest.py
conftest.py
将只包含包装器,以返回每个fixture的一个实例,并使用
@pytest.fixture
进行注释。当
conftest.py
fixture.*.py
test.*.py
文件都在同一目录中时,将夹具与测试一起使用是没有问题的

但是,当装置在子目录中分开时,我从pytest
装置“cifs”未找到中得到一个错误,
可用装置:…
。我没有找到任何文档说明如何将夹具放置在
test.*.py
之外或
test.*.py
附近的
conftest.py
之外,但也没有任何文档表明这不应该起作用

使用pytest时,如何将装置放置在其自己的子目录中?阅读如何构建测试

您的fixture目录似乎不在包中(project没有
\uuuu init\uuuu.py
因此不能作为
项目导入。fixture
作为
fixture
导入,因为路径中没有。
您可以在
tests/conftest.py
中的路径中添加所需的dir(
sys.path.append(os.path.join(os.path.dirname(uu文件),os.pardir,“fixtures”)
sys.path.append(os.path.join(os.path.dirname(u文件),os.pardirir)
取决于导入模块的方式。

尝试在
project/conftest.py

如果这就是您所说的“包装以返回实例”的意思,请在您的conftest.py中添加以下内容

导入pytest
pytest_插件=[
“fixtures.conftest”,
“fixture.fixture_cifs”,
“fixture.fixture_ftp”,
“固定装置。固定装置服务”
]
这确保了在
fixtures/
下声明的所有fixtures将被
pytest

请注意,在
fixtures.conftest“
需要有
\uuuu init\uuuu.py
文件才能加载
pytest

在这里可以看到类似的情况: