Python 接管错误报告的pytest夹具

Python 接管错误报告的pytest夹具,python,pytest,Python,Pytest,我正在编写一个用于实现回归测试的小夹具。被测函数不包含任何断言语句,但生成的输出与假定正确的记录输出进行比较 这是一个简单的片段来演示我在做什么: @pytest.yield_fixture() def regtest(请求): fp=cStringIO.StringIO() 产量fp 重置,完整路径,id=\设置(请求) 如果重置: _记录输出(fp.getvalue(),完整路径) 其他: 失败=\比较\输出(fp.getvalue(),完整\路径,请求,id) 如果失败: pytest.f

我正在编写一个用于实现回归测试的小夹具。被测函数不包含任何断言语句,但生成的输出与假定正确的记录输出进行比较

这是一个简单的片段来演示我在做什么:

@pytest.yield_fixture()
def regtest(请求):
fp=cStringIO.StringIO()
产量fp
重置,完整路径,id=\设置(请求)
如果重置:
_记录输出(fp.getvalue(),完整路径)
其他:
失败=\比较\输出(fp.getvalue(),完整\路径,请求,id)
如果失败:
pytest.fail(“回归测试%s失败”%id\ux,pytrace=False)
一般来说,我的方法是可行的,但我希望改进错误报告,以便夹具指示测试失败,而不是测试函数本身:此实现始终打印
,因为测试函数不会引发任何异常,然后在最后一行调用额外的
E
if
pytest.fail

所以我想要的是抑制被测试函数触发的
的输出,让我的fixture代码输出合适的字符

更新: 我能够改进输出,但在测试运行时,我仍然需要在输出中添加许多“.”。上载于 您可以在以下位置找到存储库:

很抱歉发布链接,但是文件现在变大了

解决方案

我通过在hook中实现一个处理regtest结果的hook,提出了一个解决方案。然后代码(简化):


\u handle\u regtest\u result
存储记录的值或进行适当的检查。该插件现在可以在

上使用,您在那里混合了两种东西:夹具本身(为测试设置条件)和预期的行为比较输出(a,b)。您可能正在寻找以下内容:

import pytest

@pytest.fixture()
def file_fixture():
    fp = cStringIO.StringIO()
    return fp.getvalue()

@pytest.fixture()
def request_fixture(request, file_fixture):
    return _setup(request)

def test_regression(request_fixture, file_fixture):
    reset, full_path, id_ = request_fixture
    if reset:
        _record_output(file_fixture, full_path)
    else:
        failed = _compare_output(file_fixture, full_path, request, id_)
        assert failed is True, "regression test %s failed" % id_

老实说,在我看来,夹具的内容应该是测试函数的内容。你在这方面还有问题吗?如果是这样的话,你能在回购协议中添加一个更具体的链接吗?是的,在这里使用固定装置是一种“误用”。从用户的角度来看,我觉得这种方法很简单。
import pytest

@pytest.fixture()
def file_fixture():
    fp = cStringIO.StringIO()
    return fp.getvalue()

@pytest.fixture()
def request_fixture(request, file_fixture):
    return _setup(request)

def test_regression(request_fixture, file_fixture):
    reset, full_path, id_ = request_fixture
    if reset:
        _record_output(file_fixture, full_path)
    else:
        failed = _compare_output(file_fixture, full_path, request, id_)
        assert failed is True, "regression test %s failed" % id_