如何清除多个断言语句之间捕获的标准输出/标准错误 [英] How to clear captured stdout/stderr in between of multiple assert statements

查看:53
本文介绍了如何清除多个断言语句之间捕获的标准输出/标准错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在循环中有一些断言测试,每个断言有点像单独的测试,我不希望以前的断言输出污染当前失败断言的错误日志.

I have some tests with assertions in loop, each assertion is kinda like separate test and I don't want previous assertions' output to pollute error logs of current failed assertion.

def test_foos(captured):
    foos = []  # some data
    for foo, bar in foos:
        captured.clear()
        assert logic(foo) == bar

我找到了 caplog.clear但它似乎不起作用.

I have found caplog.clear but it doesn't seem to work.

推荐答案

参数化你的测试.将 foos 作为参数传递,pytest 将多次运行测试 assert 行,记录成功/失败,就好像每个都是单独的测试一样.

Parametrize your test. Pass the foos as parameter and pytest will run the test assert line multiple times, recording success/failure as if each were a separate test.

import pytest

testdata = [
    (3,9),
    (4,16),
    (5,25)
]

@pytest.mark.parametrize("x,expected", testdata)
def test_foos(x, expected):
    assert logic(foo) == bar # in the example, say logic squares the input

这篇关于如何清除多个断言语句之间捕获的标准输出/标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆