36

pytest does wonderful assert introspection so it is easy to find differences in strings especially if the difference is in white space. Now I use a slightly complicated test helper that I reuse in many testcases. The helper has its own module, too and for that module I want to add assert introspection.

helpers.py:

...
def my_helper():
    assert 'abcy' == 'abcx'

test_mycase.py:

from .helpers import my_helper


def test_assert_in_tc():
    assert 'abcy' == 'abcx'


def test_assert_in_helper():
    my_helper()

test report shows helpful information for asserts within tests but not for asserts within the helper:

=============================================================== FAILURES ================================================================
___________________________________________________________ test_assert_in_tc ___________________________________________________________

    def test_assert_in_tc():
>       assert 'abcy' == 'abcx'
E       assert 'abcy' == 'abcx'
E         - abcy
E         ?    ^
E         + abcx
E         ?    ^

tests/test_pytest_assert.py:9: AssertionError
_________________________________________________________ test_assert_in_helper _________________________________________________________

    def test_assert_in_helper():
>       my_helper()

tests/test_pytest_assert.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def my_helper():
>       assert 'abcy' == 'abcx'
E       AssertionError

tests/helpers.py:258: AssertionError
======================================================= 2 failed in 0.24 seconds ========================================================

As a workaround I output additional info with the assert but the output still looks weird and makes the code blow up. Any ideas how I can activate pytest assert introspection within the helper file?

I found a different, but related question unfortunately I could not get the solution working so far:

import pytest
from .helpers import my_helper
pytest.register_assert_rewrite('helpers.my_helper')
0

3 Answers 3

30

I had to put the register_assert_rewrite into tests/__init__.py like so:

import pytest

# we want to have pytest assert introspection in the helpers
pytest.register_assert_rewrite('tests.helpers')
4
  • 6
    While this is the correct answer to the question, I thought I'd add that if you have the option, you can move the helper function into conftest.py which is registered for assert rewriting by default.
    – mcarans
    Commented Feb 9, 2022 at 1:55
  • @mcarans That's extremely helpful advice when you are stuck on a pytest version old enough to not support this answer's solution!
    – Noah
    Commented Dec 27, 2022 at 19:48
  • 1
    Unfortunately, it doesn't work correctly when you have several assert in the helper :(
    – ADR
    Commented Feb 20, 2023 at 2:23
  • pay attention that your helper can not be in the same file, where your test is
    – Andrei_N
    Commented Aug 2 at 13:42
2

Set the python_files configuration in pytest.ini:

[pytest]
python_files = python_files = tests/*.py tests/**/*.py

For more info see docs

Source

The benefit of this approach is that it works for existing and new code without having to manually register each module - provided the regex matches.

1
[pytest]
python_files = tests/*.py helpers/*.py

Worked for me, as helpers have the code where actual assertions exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.