pytest 断言消息未显示在 pycharm 中同一测试模块内的辅助函数中

问题描述 投票:0回答:3

我发现,如果断言发生在与我在 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()

想知道我是否需要申请任何设置(或解决方法)才能使其正常工作?

pycharm pytest
3个回答
0
投票

我在 PyCharm Build #PC-222.4345.23(构建于 2022 年 10 月 10 日)上测试了您的代码 它在控制台中显示失败消息。我没有在 pytest.inisetup.cfg 配置文件中定义的项目级别的任何特定设置。 enter image description here


0
投票

我有一个解决方法,尽管它使代码变得更加冗长。

def test_assertion_message_not_working():
  do_assertion()

def do_assertion():
  if not(1 == 2):
    pytest.fail("my message")

在 pycharm 中运行时提供完整的上下文信息


0
投票

将下一个附加标志添加到运行配置附加参数:--tb=short

您可以编辑配置模板以使其成为默认模板。

更多信息可以在这里找到:https://youtrack.jetbrains.com/issue/PY-49150/pytest-output-in-PyCharm-lacks-information-about-assertion-failures-in-helper-methods

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.