python - 为什么仅运行一部分测试时 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"
但是,我非常想要原始问题的答案,所以我不会删除它。
解决方案
该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
位于测试根目录中,因此它甚至会在测试会话开始之前加载(在加载通过-p
arg 传递并通过setuptools
入口点注册的插件之后)。运行pytest -s
(禁用输出捕获)应该显示位于==== test session starts ====
行上方的自定义打印。
如果您希望两次调用之间的打印内容相同,请将其放入合适的挂钩中。例如,要始终CONFTEST loaded
在测试收集完成后打印,请使用:
# api_tests/conftest.py
def pytest_collectreport(report):
print("CONFTEST loaded")
还有其他选项可用于自定义输出放置;最好是查看Hooks in pytest
reference 下的可用钩子列表。
推荐阅读
- python - DataFrame 是空的,里面有预期的数据
- python-3.x - 将不均匀字典列表转换为熊猫数据框
- java - 在 Guice 中使用注释将一个 bean 注入另一个
- icons - Leaflet-markercluster 默认图标丢失
- amcharts4 - 自定义柱栏高度的问题
- neural-network - 用于训练神经网络的 Pytorch 加载图像和地面实况图像
- javascript - 为什么立即调用的函数和非立即调用的函数在运行 return 语句时表现不同?
- oracle - 如何在 Oracle 中生成死锁场景?
- netlogo - 我怎么能让乌龟沿着路走去目的地
- ios - 从 MOV 转换为 MP4 文件后,在 AWS S3 中上传为 0 字节文件