我发现,如果断言发生在与我在 PyCharm 中的测试相同的模块内的辅助方法中,则似乎不会显示断言消息。
这似乎只发生在 PyCharm 中 - 从命令行运行时不会发生。
这有效:
def test_assertion_message_working():
assert 1 == 2, "my message"
Expected :2
Actual :1
...
def test_assertion_message_working():
> assert 1 ==2, "my message"
但这并没有(测试失败,但我没有收到失败消息):
def test_assertion_message_not_working():
do_assertion()
def do_assertion():
assert 1 == 2, "my message"
1 != 2
Expected :2
Actual :1
...
def test_assertion_not_working():
> do_assertion()
想知道我是否需要申请任何设置(或解决方法)才能使其正常工作?
我有一个解决方法,尽管它使代码变得更加冗长。
def test_assertion_message_not_working():
do_assertion()
def do_assertion():
if not(1 == 2):
pytest.fail("my message")
在 pycharm 中运行时提供完整的上下文信息
将下一个附加标志添加到运行配置附加参数:--tb=short
您可以编辑配置模板以使其成为默认模板。