2017-01-07 16 views
5

pytest robi cudowne assert introspection, więc łatwo jest znaleźć różnice w łańcuchach, szczególnie jeśli różnica jest w białej przestrzeni. Teraz używam nieco skomplikowanego pomocnika testowego, którego używam w wielu testach. Pomocnik ma również swój własny moduł i dla tego modułu chcę dodać introspekcję.Pytaj o introspekcję w funkcji pomocnika

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() 

testu pokazuje pomocne informacje dla twierdzi ciągu testów ale 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 ======================================================== 

Jako obejście generuję dodatkowe informacje z assertem, ale dane wyjściowe nadal wyglądają dziwacznie i powodują, że kod ulega awarii. Jakieś pomysły, w jaki sposób mogę aktywować pytest potwierdzają introspekcję w pliku pomocniczym?

znalazłem different, but related question niestety nie mogłem uzyskać roztwór roboczy do tej pory:

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

musiałem wstaw plik register_assert_rewrite do pliku \ _ \ _ init \ _ \ _. py. teraz działa ... prawdopodobnie najlepiej usunąć pytanie, prawda? – mark

+0

Nie usuwaj pytania, właśnie napotkałem ten problem. Tak więc. – alejandrodnm

Odpowiedz

3

musiałem umieścić register_assert_rewrite do testów/__ init__.py tak:

import pytest 

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

Możesz zaakceptować jako poprawną odpowiedź. – alejandrodnm

Powiązane problemy