我有一个简单的测试代码:
试验_断言.py在
import helper
def test_assert():
assert helper.find_event(helper.HelperEvents("event","this is info")) is True
在助手.py在
^{pr2}$这会给我一个这样的断言错误:
def test_assert():
> assert helper.find_event(helper.HelperEvents("event","this is info")) is True
E AssertionError: assert False is True
E + where False = <function find_event at 0x158c9b0>(<test_asserts.helper.HelperEvents instance at 0x157d488>)
E + where <function find_event at 0x158c9b0> = helper.find_event
E + and <test_asserts.helper.HelperEvents instance at 0x157d488> = <class test_asserts.helper.HelperEvents at 0x1670188>('event', 'this is info')
E + where <class test_asserts.helper.HelperEvents at 0x1670188> = helper.HelperEvents
test_assert.py:5: AssertionError
但我真的想把它简化成这样: AssertionError:事件:事件信息:在列表中未找到此信息
为此,我创建了一个init.py文件
import pytest
pytest.register_assert_rewrite('test_asserts')
还有一个conftest.py文件:
import pytest
@pytest.hookimpl(tryfirst=True)
def pytest_assertrepr_compare(op, left, right):
print "Left:", left
print "op:", op
print "Right:", right
return [str(left), op, str(right)]
我得到了以下结论:
def test_assert():
> assert helper.find_event(helper.HelperEvents("event","this is info")) is True
E assert False
E is
E True
test_assert.py:5: AssertionError
当我只看到find_事件调用的结果时,如何从事件中提取信息?在
所以我最终实现了pytest_runtest_makereport的钩子。 我重用了pytests实现中的代码,并用我自己的自定义assert表示对其进行了扩展。 看起来像这样:
所有这些代码都放在conftest.py马上。 如果断言是一个不能被实现识别的类型,它将从my hook返回None,这将使pytest使用默认实现。在
相关问题 更多 >
编程相关推荐