We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest-check - allow multiple failures per test

pytest-check - allow multiple failures per test

2025/5/2
logo of podcast Test & Code

Test & Code

AI Deep Dive Transcript
People
我 (作者)
Topics
我创建了 pytest-check 插件,它允许在单个测试函数中出现多个失败的断言。 传统的测试方法通常在遇到第一个失败的断言时就停止运行,但这在某些情况下会限制我们对测试结果的全面了解。pytest-check 插件则允许测试函数继续运行,并报告所有失败的检查结果,这对于那些需要检查多个条件的测试用例非常有用。 插件提供了两种主要的使用方式:上下文管理器和验证函数。上下文管理器 `with check:` 可以将多个断言语句包含在内,即使其中一些失败,也能继续执行后续的断言。验证函数则提供了一系列类似于 assert 的函数,例如 `equal`、`not equal` 等,方便进行各种类型的比较。 插件会收集所有失败的检查,并在测试报告中显示每个失败检查的伪回溯信息,方便调试。为了避免过多的输出信息影响效率,插件还允许用户设置限制,例如限制回溯信息的显示数量、失败报告的数量以及失败检查的总数。这些限制可以通过命令行标志或配置文件进行设置。 插件的默认设置已经足够高效,但用户可以根据需要调整这些参数。此外,用户还可以根据自己的需求自定义 helper 函数,以扩展插件的功能。 我开发这个插件的初衷是解决在测试中需要检查多个条件的场景。在单元测试中,通常建议每个测试只检查一个条件,但这在实际应用中过于限制。pytest 不仅用于单元测试,也用于大型系统测试,例如测试财务软件的转账功能或测试射频测试设备的波形。在这些场景中,需要检查多个条件,而 pytest-check 插件可以更好地满足这些需求,提供更全面的测试结果。

Deep Dive

Shownotes Transcript

pytest-check is a pytest plugin that allows multiple failures per test.

Normally, a test function will fail and stop running with the first failed assert. That's totally fine for tons of kinds of software tests. However, there are times where you'd like to check more than one thing, and you'd really like to know the results of each check, even if one of them fails.

pytest-check allows multiple failed "checks" per test function, so you can see the whole picture of what's going wrong.

Links:

**Sponsored by: **

**Learn pytest: **

  • The Complete pytest course) is now a bundle, with each part available separately.

  • pytest Primary Power) teaches the super powers of pytest that you need to learn to use pytest effectively.

  • Using pytest with Projects) has lots of "when you need it" sections like debugging failed tests, mocking, testing strategy, and CI

  • Then pytest Booster Rockets) can help with advanced parametrization and building plugins.

  • Whether you need to get started with pytest today, or want to power up your pytest skills, PythonTest) has a course for you.

** ★ Support this podcast on Patreon ★) **