将第三方库转换为真正的 pytest 插件
问题背景
在使用 pytest 进行测试时,我遇到了这样的错误:
1 | Defining 'pytest_plugins' in a non-top-level conftest is no longer supported: It affects the entire test suite instead of just below the conftest as expected. |
这个错误通常出现在测试工程的结构中有多层 conftest.py 文件,并且在非顶层的 conftest 中定义了 pytest_plugins。从 pytest 7.0.0 版本开始,这种用法被废弃,因为它会影响整个测试套件而不仅仅是该 conftest.py 以下的测试。
案例中,测试工程根目录下有一个 conftest.py,其中包含:
1 | pytest_plugins = ["my_python_lib.base.testbase.conftest"] |
这里 my_python_lib 是一个自定义的 Python 第三方库,测试工程中的用例需要调用 my_python_lib.base.testbase.conftest 中的 fixture。
最佳解决方案:将库转换为真正的 pytest 插件
将我们的库转换为一个真正的 pytest 插件是最优雅和最可维护的解决方案。这样不仅解决了当前问题,还提高了代码的可复用性和可扩展性。
步骤 1:重构库结构
首先,调整库结构,确保 fixture 代码位于合适的模块中:
1 | my_python_lib/ |
步骤 2:创建插件入口点文件
创建 plugin.py 文件,导入所有 fixture 并定义任何需要的 pytest 钩子:
1 | # my_python_lib/base/testbase/plugin.py |
步骤 3:修改库的 setup.py
在库的 setup.py 中添加 pytest 插件的入口点:
1 | from setuptools import setup, find_packages |
步骤 4:重新安装库
1 | pip uninstall -y my_python_lib # 先卸载当前版本 |
步骤 5:修改测试项目
删除测试项目中 conftest.py 中的 pytest_plugins 定义,因为现在插件会自动加载:
1 | # 测试项目的 conftest.py |
步骤 6:验证插件是否正确安装
运行以下命令验证插件是否被正确识别:
1 | python -m pytest --trace-config |
应该能看到类似这样的输出:
1 | pytest11 plugin registration SETUP: my_python_lib.base.testbase.plugin |
代码示例
fixture.py 示例
1 | # my_python_lib/base/testbase/fixture.py |
plugin.py 完整示例
1 | # my_python_lib/base/testbase/plugin.py |
测试示例
1 | # 测试文件示例 test_utils.py |
使用方法
安装了这个 pytest 插件后,你可以在任何测试项目中直接使用这些 fixture,无需额外导入或配置:
安装你的库:
1
pip install my_python_lib
在测试文件中直接使用 fixture:
1
2
3
4
5
6def test_file_operations(temp_dir, temp_file):
# 自动获取临时目录和临时文件
with open(temp_file, 'w') as f:
f.write('测试内容')
assert os.path.exists(temp_file)使用示例数据 fixture:
1
2
3
4def test_data_processing(sample_data):
# sample_data 自动可用
assert sample_data["name"] == "test"
assert len(sample_data["values"]) == 5跳过慢测试:
1
python -m pytest --skip-slow
运行测试并查看所有可用的标记:
1
python -m pytest --markers
这些 fixture 可以组合使用,也可以在自己的 conftest.py 中扩展它们,为它们提供自定义行为。
优势
- 符合 pytest 最佳实践 - 使用官方推荐的插件机制
- 避免警告和错误 - 不再使用不推荐的
pytest_plugins方式 - 更好的可发现性 - 自动注册 fixture,无需显式导入
- 可配置性 - 可以添加命令行选项和配置项
- 模块化 - 更容易维护和扩展
- 可重用性 - 可以在多个项目中使用同一套测试工具
总结
通过将测试工具库转换为真正的 pytest 插件,我们不仅解决了特定的错误问题,还提高了代码质量和可维护性。这种方法虽然前期工作量稍大,但从长远来看更加健壮,尤其是当测试库需要在多个项目中使用时。