[英]Why doesn't pytest load conftest.py when running only a subset of tests?
Here is my API test directory layout:这是我的 API 测试目录布局:
api_tests
├── conftest.py
└── query
└── me_test.py
Contents of conftest.py: conftest.py 的内容:
print("CONFTEST LOADED")
Contents of me_test.py: me_test.py 的内容:
"""Tests the "me" query"""
def test_me():
assert True
If I simply run pytest
, everything works:如果我只是运行
pytest
,一切正常:
================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collecting ... CONFTEST LOADED
collected 3 items
api_tests/query/me_test.py . [ 33%]
lib/myproject/utils_test.py . [ 66%]
lib/myproject/schema/types/scalars_test.py .
Notice "CONFTEST LOADED" is printed.注意打印“CONFTEST LOADED”。 Great!
伟大的! However, this test run also picked up all my unit tests, which I don't want.
但是,此测试运行还包含了我不想要的所有单元测试。 I want to separate my test runs into unit tests and API tests, I don't want to run them all in one go.
我想将我的测试运行分为单元测试和 API 测试,我不想一次性运行它们。
However, if I simply run pytest api_tests/
:但是,如果我只是运行
pytest api_tests/
:
================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collected 1 item
api_tests/query/me_test.py . [100%]
================================================== 1 passed in 0.00s ==================================================
Now the right tests are run, but the conftest.py file wasn't loaded... How come?现在运行了正确的测试,但是没有加载 conftest.py 文件......怎么会?
I am using Pytest 6.1.0 on Python 3.8.我在 Python 3.8 上使用 Pytest 6.1.0。
EDIT: Alright, I found an acceptable workaround.编辑:好的,我找到了一个可以接受的解决方法。 I can override INI file options through the command line with the
-o
option.我可以通过命令行使用
-o
选项覆盖 INI 文件选项。 This works:这有效:
poetry run pytest -o "testpaths=api_tests"
However, I would very much like an answer to the original question so I'm not going to delete it.但是,我非常想要原始问题的答案,因此我不会将其删除。
The conftest
plugin will be registered in both invocations, the only difference being the registration stage. conftest
插件将在两次调用中注册,唯一的区别是注册阶段。 If in doubt, add the --traceconfig
argument to list the registered plugins in order of their registration:如果有疑问,请添加
--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'>
...
In the first invocation, the conftest.py
won't be found immediately since it's down the test root path, so it will be loaded while pytest
discovers the tests.在第一次调用中,
conftest.py
不会立即被找到,因为它位于测试根路径下,所以它会在pytest
发现测试时加载。 In the second invocation, conftest.py
is located in test root, so it will be loaded even before the test session starts (after the plugins passed via -p
arg and registered via setuptools
entrypoint are loaded).在第二次调用中,
conftest.py
位于测试根目录中,因此它甚至会在测试会话开始之前加载(在加载通过-p
arg 传递并通过setuptools
入口点注册的插件之后)。 Running pytest -s
(with output capturing disabled) should reveal the custom print, located above the ==== test session starts ====
line.运行
pytest -s
(禁用输出捕获)应该显示位于==== test session starts ====
行上方的自定义打印。
If you want the print to be identical between the two invocations, put it in a suitable hook.如果您希望两次调用之间的打印相同,请将其放在合适的挂钩中。 For example, to always print
CONFTEST loaded
after test collection finished, use:例如,要在测试收集完成后始终打印
CONFTEST loaded
,请使用:
# api_tests/conftest.py
def pytest_collectreport(report):
print("CONFTEST loaded")
There are other options available for custom output placement;还有其他选项可用于自定义输出放置; best is to check out the list of available hooks under Hooks in
pytest
reference.最好是查看
pytest
参考中Hooks下的可用钩子列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.