pytest确实很棒,assert introspection因此很容易找到字符串的差异,特别是如果差异在白色空间中.现在我使用了一个稍微复杂的测试助手,我在许多测试用例中重用了它.帮助器也有自己的模块,对于那个模块,我想添加断言内省.
helpers.py:
...
def my_helper():
assert 'abcy' == 'abcx'
Run Code Online (Sandbox Code Playgroud)
test_mycase.py:
from .helpers import my_helper
def test_assert_in_tc():
assert 'abcy' == 'abcx'
def test_assert_in_helper():
my_helper()
Run Code Online (Sandbox Code Playgroud)
测试报告显示测试中断言的有用信息,但是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 ========================================================
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我使用断言输出附加信息,但输出看起来仍然很奇怪并使代码爆炸.任何想法如何激活pytest断言辅助文件中的内省?
我发现了一个不同但相关的问题,遗憾的是我到目前为止无法解决这个问题:
import pytest
from .helpers import my_helper
pytest.register_assert_rewrite('helpers.my_helper')
Run Code Online (Sandbox Code Playgroud)
mar*_*ark 14
我不得不将register_assert_rewrite放入tests/__ init__.py中,如下所示:
import pytest
# we want to have pytest assert introspection in the helpers
pytest.register_assert_rewrite('tests.helpers')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1256 次 |
| 最近记录: |