我正在寻找创建一个pytest结构,我可以完全分离灯具和测试。这种分离的原因是我希望将fixtures目录作为外部项目包含在subversion中,并在多个项目之间共享。
所需结构的树
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
将包含包装器以返回用@pytest.fixture
注释的每个夹具的实例。当conftest.py
,fixture_*.py
和test_*.py
文件都在同一目录中时,使用夹具和测试没有问题。
但是,当灯具在子目录中分离时,我从pytest fixture 'cifs' not found
,available fixtures: ...
得到一个错误。我没有找到任何文件解释如何在test_*.py
附近放置灯具或conftest.py
附近的test_*.py
,但没有任何迹象表明这也不起作用。
使用pytest时如何将灯具放在各自的子目录中?
阅读here如何构建您的测试。
你的fixture目录似乎不在包中(项目没有__init__.py
所以不能作为project.fixtures
导入,因为fixtures
不在路径中。你可以在你的tests/conftest.py
(sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, "fixtures"))
或sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)
)路径中添加所需的dirs,具体取决于你想如何导入你的模块。
请添加你的conftest.py
import pytest
pytest_plugins = [
"fixtures.conftest",
"fixtures.fixture_cifs",
"fixtures.fixture_ftp",
"fixtures.fixture_service"
]
所有声明的额外灯具将由pytest
创立
请注意,
fixtures.conftest"
中提到的各个目录需要有__init__.py
文件才能由pytest
加载插件
请在这里阅读类似的案例:https://stackoverflow.com/a/54736237/6655459
你在import
尝试过project/conftest.py
ing你的灯具吗?这就是“返回实例的包装器”的意思吗?