即使多次失败也运行所有 PyTest 断言(没有拆分测试的选项)

问题描述 投票:0回答:1

我正在使用 PyTest,我想确保测试运行中的所有断言,即使其中一些断言失败。不幸的是,我无法选择将每个断言的测试分解为单独的测试。

在 PyTest 中是否有推荐的方法来实现此行为?

我正在使用以下版本:

  • Python:3.8.10
  • Pytest 8.1.1
import subprocess

import pytest

def test_executable_script():
result = subprocess.check_output([executable_script], encoding="UTF-8")
assert result != "", f"Error: {result}"

result_2 = subprocess.check_output([another_executable_script], encoding="UTF-8")
assert result != "", f"Error: {result}"

python pytest assertion
1个回答
0
投票

您可以创建失败列表并在该列表上断言。以下是如何实现它。

import subprocess
import pytest


def test_executable_script():
    _fails = []

    result = subprocess.check_output([executable_script], encoding="UTF-8")
 
    if result == "":
        _fails.append("Error1")

    result_2 = subprocess.check_output([another_executable_script],encoding="UTF-8")

    if result_2 == "":
        _fails.append("Error2")

    assert not _fails, "Test failed because of: {}".format(" - ".join(_fails))
© www.soinside.com 2019 - 2024. All rights reserved.