python - 如何清除多个断言语句之间捕获的 stdout/stderr

标签 python pytest

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

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

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

最佳答案

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

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

关于python - 如何清除多个断言语句之间捕获的 stdout/stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187165/

相关文章:

Python 使用 BinarySecurityToken 签署 SOAP 请求

python - 如何在 python OpenCV 中打开 CR2 文件

python - 如何显示不带引号且以逗号作为分隔符的 NumPy 字符串数组?

python - Tornado celery 不能使用 gen.Task 或 CallBack

python - Travis CI 默认使用错误的语言

python - 使用 pytest 将测试拆分为不同的函数

python - py.test : Temporary folder for the session scope

python - pytest - 辅助函数或 fixture 、参数化

python - 计算每个键的唯一值的有效方法

python - 如何测试 SocketIO 服务器连接(使用 pytest 或任何其他包)?