pytest assertion message not displayed in helper function within same test module in pycharm
Asked Answered
A

3

6

I'm seeing that assertion messages do not seem to be displayed if the assertion occurs in a helper method within the same module as my test when in PyCharm.

This only seems to be happening in PyCharm - it does not happen when running from the command line.

This works:

def test_assertion_message_working():
  assert 1 == 2, "my message"
Expected :2
Actual :1
...
def test_assertion_message_working():
> assert 1 ==2, "my message"

But this doesn't (the test fails, but I do not get the failure 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()

Wondering if there is any setting (or workaround) I need to apply to get this working?

Adelaidaadelaide answered 28/10, 2022 at 11:36 Comment(1)
this is an actual issue in pycharm: youtrack.jetbrains.com/issue/PY-50396Barbour
M
2

Add the next additional flag to the run configuration Additional Arguments: --tb=short

You can edit the configuration templates to make it the default.

More info can be found here: https://youtrack.jetbrains.com/issue/PY-49150/pytest-output-in-PyCharm-lacks-information-about-assertion-failures-in-helper-methods

enter image description here

Michelemichelina answered 10/6 at 12:45 Comment(0)
P
0

I tested your code on PyCharm Build #PC-222.4345.23, built on October 10, 2022 and it shows failure message in console. I do not have any specific settings on the project level defined in pytest.ini or setup.cfg config files. enter image description here

Potassium answered 9/11, 2022 at 14:26 Comment(2)
I am reproducing this exact problem in Pycharm Build #PC-232.10227.11, built on November 13, 2023.Potential
You are running from the command line that is inside PyCharm, that is not what OP meant. He Is probably running by clicking the Play button next to the test function (after configuring default tests to run with pytest).Michelemichelina
P
0

I have a workaround though it makes the code a bit more verbose.

def test_assertion_message_not_working():
  do_assertion()

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

Which does give the full context information when run in pycharm

Potential answered 22/11, 2023 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.