接管错误报告的pytest fixture

2024-06-28 20:06:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个实现回归测试的小程序。被测函数不包含任何assert语句,但生成的输出将与假定为正确的记录输出进行比较。在

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

@pytest.yield_fixture()
def regtest(request):

    fp = cStringIO.StringIO()

    yield fp

    reset, full_path, id_ = _setup(request)
    if reset:
        _record_output(fp.getvalue(), full_path)
    else:
        failed = _compare_output(fp.getvalue(), full_path, request, id_)
        if failed:
            pytest.fail("regression test %s failed" % id_, pytrace=False)

一般来说,我的方法是可行的,但是我想改进错误报告,以便fixture指示测试的失败,而不是测试函数本身:这个实现总是打印一个.,因为测试函数不会引发任何异常,然后在最后一行调用一个额外的Eif{}。在

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

更新: 我能够提高输出,但在测试运行时,我仍然需要在输出中添加许多“.”。它上载在https://pypi.python.org/pypi/pytest-regtest 您可以在https://sissource.ethz.ch/uweschmitt/pytest-regtest/tree/master找到存储库

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

解决方案

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

^{pr2}$

_handle_regtest_result存储记录的值或执行适当的检查。该插件现在在https://pypi.python.org/pypi/pytest-regtest上可用


Tags: path函数httpspypiidpytestrequest记录
1条回答
网友
1楼 · 发布于 2024-06-28 20:06:11

您在那里混合了两个东西:fixture本身(为测试设置条件)和预期的行为\u compare\u输出(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_

相关问题 更多 >