8

If the second parameter of assert is used normally it prints it out debug information. E.g. with the following code:

class TestTest:
    def test_test(self):
        assert 1 == 2, "TEST"

The following debug information is printed:

tests\test_test.py:1 (TestTest.test_test)
1 != 2

Expected :2
Actual   :1
<Click to see difference>

self = <tests.test_test.TestTest object at 0x000002147E5B1B08>

    def test_test(self):
>       assert 1 == 2, "TEST"
E       AssertionError: TEST
E       assert 1 == 2
E         +1
E         -2

test_test.py:3: AssertionError

However, if the assert happens in a helper function, this is not printed:

class TestTest:
    def test_test(self):
        self.verify(1)

    def verify(self, parameter):
        assert 1 == 2, "TEST"

results in:

tests\test_test.py:1 (TestTest.test_test)
1 != 2

Expected :2
Actual   :1
<Click to see difference>

self = <tests.test_test.TestTest object at 0x000002AFDF1D5AC8>

    def test_test(self):
>       self.verify(1)

test_test.py:3:

So the stack trace is incomplete and the debug information not shown.

UPDATE: This only happens when starting the test through PyCharm (2021.3).

Is there any way to fix this?

Klaus Stadler
  • 395
  • 3
  • 17
  • Does this help? https://stackoverflow.com/questions/41522767/pytest-assert-introspection-in-helper-function – tmt Dec 03 '21 at 11:21
  • Which version of pytest do you use? With version 6.2.5 I can't reproduce incomplete stack trace. – Kaz Dec 03 '21 at 13:40
  • I've updated to 6.2.5, same problem, but I noticed it doesn't happen when starting pytest trough the command line instead of trough PyCharm, Question updated. – Klaus Stadler Dec 06 '21 at 07:54
  • In PyCharm settings, which is your testing default test runner? (in File | Settings | Tools | Python Integrated Tools) – Kaz Dec 06 '21 at 08:55
  • it says autodectect (pytest) – Klaus Stadler Dec 07 '21 at 09:32
  • 3
    PyCharm calls pytest with a bunch of quieting options:`pytest --no-header --no-summary -q`. Does [the solution shown in this other SO thread](https://stackoverflow.com/a/73873315) help? – Savir Mar 22 '23 at 21:29
  • Indeed, this works. The only drawback is that you have to click on "Test Results" to see the assert diff. It is not captured in the output of the failing test itself. – DevOps Craftsman Mar 27 '23 at 04:55

1 Answers1

1

Pycharm has some quieting options, such as pytest --no-header --no-summary -q

They can be configured: File > Settings > Advanced Settings and then checking the Pytest: do not add... I think you'll understand.

  • It’s a shame that you have to click on the top level "Test Results" to see the output… (not captured in the output of the failing test itself) – DevOps Craftsman Mar 27 '23 at 04:55