Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with assert when using helper method in pytest when starting tests trough PyCharm

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?

like image 708
Klaus Stadler Avatar asked May 03 '26 19:05

Klaus Stadler


2 Answers

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.

Adding --tb=short in your test configuration does display the whole stack trace in the single test result view. This include the helper assert method.

coming from the bug report on that issue

like image 35
Jonatan Cloutier Avatar answered May 05 '26 08:05

Jonatan Cloutier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!