Pytest claims introspection in helper function

pytest makes a wonderful assert introspection , so it's easy to find differences in strings, especially if the difference is in white space. Now I use a slightly complicated test assistant, which I use in many test files. The helper has its own module, and for this 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 useful information for assertions in 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 information using assert, but the output still looks strange and leads to a code explosion. Any ideas how I can activate pytest claim introspection in a helper file?

I found another, but related question , unfortunately, I could not get the solution to work so far:

 import pytest from .helpers import my_helper pytest.register_assert_rewrite('helpers.my_helper') 
+5
source share
1 answer

I had to insert register_assert_rewrite into the / __ init__.py tests, for example:

 import pytest # we want to have pytest assert introspection in the helpers pytest.register_assert_rewrite('tests.helpers') 
+3
source

Source: https://habr.com/ru/post/1262465/


All Articles