phpUnit 显示运行的测试数和作出的断言数。我目前执行python的单元测试的方式,只显示运行的测试数量。有没有办法计算断言的数量?
最佳答案
如果您愿意使用 pytest 运行测试,它可以为您计算断言。
有一个hook您可以在您的 conftest.py
中实现为每个通过的断言调用的文件。您可以计算被调用的次数,然后在摘要中打印出来。这不包括对 unittest.TestCase.assert...()
的调用函数,仅 assert
陈述。
要启用该 Hook ,请编写 conftest.py
像这样的文件:
assertion_count = 0
def pytest_assertion_pass(item, lineno, orig, expl):
global assertion_count
assertion_count += 1
def pytest_terminal_summary(terminalreporter, exitstatus, config):
print(f'{assertion_count} assertions tested.')
然后在您的
pytest.ini
中启用 Hook 文件:[pytest]
enable_assertion_pass_hook=true
您还可以找到 assertion compare hook有用。
毕竟,我不确定断言的数量是否是测试质量的可靠衡量标准。您可能希望使用 MutPy 进行突变测试或 mutmut .
关于python - 在 python 单元测试中显示断言数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20811656/