首页 > 解决方案 > 为什么仅运行一部分测试时 pytest 不加载 conftest.py?

问题描述

这是我的 API 测试目录布局:

api_tests
├── conftest.py
└── query
    └── me_test.py

conftest.py 的内容:

print("CONFTEST LOADED")

me_test.py 的内容:

"""Tests the "me" query"""

def test_me():
    assert True

如果我只是运行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 .                                                                    

注意打印“CONFTEST LOADED”。伟大的!然而,这个测试运行也拿起了我所有的单元测试,这是我不想要的。我想将我的测试运行分成单元测试和 API 测试,我不想一次性运行它们。

但是,如果我只是运行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 ==================================================

现在运行了正确的测试,但是没有加载 conftest.py 文件……怎么会?


我在 Python 3.8 上使用 Pytest 6.1.0。


编辑:好的,我找到了一个可以接受的解决方法。我可以通过带有选项的命令行覆盖 INI 文件-o选项。这有效:

poetry run pytest -o "testpaths=api_tests"

但是,我非常想要原始问题的答案,所以我不会删除它。

标签: pythonpytest

解决方案


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'>
...

在第一次调用中,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")

还有其他选项可用于自定义输出放置;最好是查看Hooks in pytestreference 下的可用钩子列表。


推荐阅读