这是我的 API 测试目录布局:
\napi_tests\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conftest.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 query\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 me_test.py\nRun Code Online (Sandbox Code Playgroud)\nconftest.py的内容:
\napi_tests\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conftest.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 query\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 me_test.py\nRun Code Online (Sandbox Code Playgroud)\nme_test.py 的内容:
\nprint("CONFTEST LOADED")\nRun Code Online (Sandbox Code Playgroud)\n如果我只是运行pytest,一切正常:
================================================= test session starts =================================================\nplatform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1\nrootdir: /home/hubro/myproject, configfile: pytest.ini\ncollecting ... CONFTEST LOADED\ncollected 3 items \n\napi_tests/query/me_test.py . [ 33%]\nlib/myproject/utils_test.py . [ 66%]\nlib/myproject/schema/types/scalars_test.py . \nRun Code Online (Sandbox Code Playgroud)\n注意打印“CONFTEST LOADED”。伟大的!然而,这个测试运行也获取了我不想要的所有单元测试。我想将我的测试运行分为单元测试和 API 测试,我不想一次性运行它们。
\n但是,如果我只是运行pytest api_tests/:
================================================= test session starts =================================================\nplatform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1\nrootdir: /home/hubro/myproject, configfile: pytest.ini\ncollected 1 item \n\napi_tests/query/me_test.py . [100%]\n\n================================================== 1 passed in 0.00s ==================================================\nRun Code Online (Sandbox Code Playgroud)\n现在运行了正确的测试,但是 conftest.py 文件没有加载......这是怎么回事?
\n我在 Python 3.8 上使用 Pytest 6.1.0。
\n编辑:好吧,我找到了一个可接受的解决方法。我可以通过命令行使用该选项覆盖 INI 文件选项-o。这有效:
poetry run pytest -o "testpaths=api_tests"\nRun Code Online (Sandbox Code Playgroud)\n然而,我非常想要原始问题的答案,所以我不会删除它。
\n该conftest插件将在两次调用中注册,唯一的区别是注册阶段。如果有疑问,请添加--traceconfig参数以按注册顺序列出已注册的插件:
$ pytest --traceconfig
PLUGIN registered: <_pytest.config.PytestPluginManager object at 0x7f23033ff100>
PLUGIN registered: <_pytest.config.Config object at 0x7f2302d184c0>
...
=================================== test session starts ===================================
...
PLUGIN registered: <module 'conftest' from 'path/to/conftest.py'>
...
Run Code Online (Sandbox Code Playgroud)
在第一次调用中,conftest.py不会立即找到它,因为它位于测试根路径下,因此将在pytest发现测试时加载它。在第二次调用中,conftest.py位于测试根目录中,因此即使在测试会话开始之前也会加载它(在加载通过-parg 传递并通过setuptools入口点注册的插件之后)。运行pytest -s(禁用输出捕获)应该会显示位于该==== test session starts ====行上方的自定义打印。
如果您希望两次调用之间的打印内容相同,请将其放入合适的挂钩中。例如,要始终CONFTEST loaded在测试收集完成后打印,请使用:
# api_tests/conftest.py
def pytest_collectreport(report):
print("CONFTEST loaded")
Run Code Online (Sandbox Code Playgroud)
还有其他选项可用于自定义输出放置;最好是查看参考中的Hookspytest下的可用钩子列表。