使用 pytest 覆盖率,有没有办法判断哪些测试调用了特定语句?

Using pytest coverage, is there a way to tell which tests invoked a particular statement?

我正在查看由 pytest-cov 生成的 html 覆盖率报告,我想知道我是否可以找到哪些测试调用了特定行(哪个测试覆盖了特定行)。

Coverage.py 包含一个称为“上下文”的功能,可以轻松地使用测试名称注释结果:https://coverage.readthedocs.io/en/latest/contexts.html

将其放入您的 .coveragerc 文件中:

[run]
dynamic_context = test_function