Python 帮助函数中的pytest断言内省

Python 帮助函数中的pytest断言内省,python,pytest,Python,Pytest,pytest做得很好断言内省,因此很容易找到字符串中的差异,尤其是在空白处。现在我使用了一个稍微复杂的测试助手,我在许多测试用例中重用了它。助手也有自己的模块,对于该模块,我想添加断言内省 助手。py: ... def my_helper(): assert 'abcy' == 'abcx' from .helpers import my_helper def test_assert_in_tc(): assert 'abcy' == 'abcx' def test_a

pytest
做得很好
断言内省
,因此很容易找到字符串中的差异,尤其是在空白处。现在我使用了一个稍微复杂的测试助手,我在许多测试用例中重用了它。助手也有自己的模块,对于该模块,我想添加断言内省

助手。py:

...
def my_helper():
    assert 'abcy' == 'abcx'
from .helpers import my_helper


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


def test_assert_in_helper():
    my_helper()
测试\u mycase.py:

...
def my_helper():
    assert 'abcy' == 'abcx'
from .helpers import my_helper


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


def test_assert_in_helper():
    my_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 ========================================================
作为一种解决方法,我使用assert输出了额外的信息,但输出结果看起来仍然很奇怪,并且使代码膨胀。你知道如何在助手文件中激活pytest断言内省吗

我发现了一个问题,很遗憾,到目前为止,我无法使解决方案起作用:

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

我必须将寄存器\u assert\u rewrite放入tests/\uuuuu init\uuuuuuuuy.py,如下所示:

import pytest

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

我必须将寄存器\u assert\u rewrite放入\uuuu init\uuuuu.py文件中。现在它工作了。。。也许最好删除这个问题,对吧?不要删除这个问题,我只是面对这个问题。所以thx。