python - 最近是否有一个带有简单 "assert"的 pytest 替代方案?

标签 python pytest

我想使用 pytest 之类的东西来使用简单的断言进行非常基本的测试。 pytest 是最佳选择还是最近有更好的替代方案?

最佳答案

这有两个方面 - 就实际的替代测试库而言,pytest 几乎是行业标准,没有明显的竞争对手。我找不到任何其他具有“simple assert”风格 api 的测试框架。

现在,如果您正在寻找比 pytest 更简单的东西,assert 实际上是 python builtin ,您可以编写自己的微型运行者:

def run_tests(**test_fns):
     failures = dict()
     for test_name, test_fn in test_fns.items():
         try:
             test_fn()
         except Exception as e:
             failures[test_name] = e
     return failures

def failing_test():
     assert False, 'failing test failed'

run_tests(fail=failing_test)
#> {'fail': AssertionError('failing test failed')}
# then maybe pretty print the failures and exit properly

您也可以考虑doctest如果您试图避免使用第三方库,因为它包含在标准库中。

关于python - 最近是否有一个带有简单 "assert"的 pytest 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678559/

相关文章:

python - 通过 pytest 使用多处理时如何测量覆盖率?

python - 如何以模块化的方式设计应用程序?

python - 安装 Panda3d 以使用 python 运行

python - Pytest:在测试结束时运行一个函数

python - 单独目录中的 pytest fixtures

python - 如何使用 "pytest"中另一个文件中的函数和类?

python - 有效区分元组中不同可能的组合

python - 文本分类性能

python - 如何使 python 脚本可执行?

python - 如何在 pytest 中断言 ValueError