我最近了解了使用 Pytest 在 Python 中进行单元测试。我在 Repl.it 中玩过它通过对两个应该失败的不同字符串进行简单的断言。
import pytest
assert 'a' == 'b'
哪个因错误而失败
Traceback (most recent call last):
File "python", line 2, in <module>
AssertionError
但是 Pytest 不打印 'a' 或 'b'。这对调试非常有帮助,因为我可以比较 2 个不同字符串之间的差异。 unittest 模块默认具有此功能。 Pytest 有这个功能吗?如果没有,我该如何启用它?我正在将 Pytest 用于一个更大的项目,并且很想在继续工作之前弄清楚这一点。
最佳答案
另见 assertion rewriting如果您的断言代码在外部辅助函数中。
这对我来说是个问题,但通过添加 pytest.register_assert_rewrite('path.to.helper')
解决了在 __init__.py
在测试目录中。
关于python - Pytest 断言在 AssertionError 上没有显示差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001496/