We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest-cov : The pytest plugin for measuring coverage

pytest-cov : The pytest plugin for measuring coverage

2025/1/23
logo of podcast Test & Code

Test & Code

AI Deep Dive AI Chapters Transcript
People
主讲人
Topics
主讲人: 我主要介绍了 pytest-cov 插件和 Coverage.py 的结合使用,以及如何提高 Python 代码的测试覆盖率。Coverage.py 是一个用于测量代码覆盖率的工具,它可以监控程序执行,识别未执行的代码,并生成多种类型的报告,包括文本报告和 HTML 报告。pytest-cov 插件则简化了 Coverage.py 的使用流程,并提供了许多额外的功能,例如子进程支持、XDist 插件支持以及上下文支持。 在实际使用中,我通常先使用 Coverage.py 生成文本报告用于 CI 和本地快速检查,如果发现未覆盖的行较多,则会使用 HTML 报告进行更详细的分析。对于不需要覆盖的代码,我会在代码中添加 `pragma no cover` 注解或在配置文件中列出这些文件。我追求 100% 的代码覆盖率,但会先进行高层次系统测试,再根据实际情况决定是否需要测试未覆盖的代码,并对不需要测试的代码进行记录或排除。 pytest-cov 插件的优势在于它简化了覆盖率分析流程,避免了手动运行 `coverage run` 和 `coverage report` 命令。它还支持子进程和 XDist 插件,并能自动合并并行测试的报告。此外,它还允许设置 `--cov-fail-under` 参数,指定覆盖率下限,低于该下限则测试套件失败。 Coverage.py 的上下文支持功能可以显示每行代码是由哪些测试覆盖的,pytest-cov 插件简化了该功能的配置,并能在 HTML 报告中显示每行代码对应的测试。这在调试时非常有用。总而言之,pytest-cov 和 Coverage.py 的结合使用可以有效提高 Python 代码的测试覆盖率,并简化测试流程。

Deep Dive

Chapters
This chapter introduces Coverage.py, a tool for measuring code coverage in Python. It explains the importance of measuring coverage in both source and test code, highlighting the benefits of identifying missed lines, branches, and the configuration options available for customizing coverage reports. Different reporting options such as text-based and HTML reports are discussed.
  • Coverage.py measures code coverage of Python programs.
  • It identifies executed and unexecuted code parts.
  • Provides per-file and branch coverage reports.
  • Offers various reporting options (text, HTML) and configuration for exclusions.

Shownotes Transcript

pytest-cov is a pytest plugin that helps produce coverage reports using Coverage.py.

In this episode, we'll discuss:

  • what Coverage.py is
  • why you should measure code coverage on both your source and test code
  • what pytest-cov is
  • extra features pytest-cov gives you over and above coverage.py
  • and generally why using both is awesome

Links:

** Learn pytest**