我想使用 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/