首页 > 解决方案 > 如何在 Unittest 中使用多个失败并将它们打印为失败而不结束测试?

问题描述

我需要用几个程序来编写一个测试来检查几个用例(步骤)。对于每个步骤,它可能是一个子步骤。每个步骤/子步骤都在检查其他内容,例如:

Procedure 1 :
 Step 1 : Verify something
 Step 2 : Compare ....
  Sub-step 2.1 : Compare ...
  Sub-step 2.2 : Compare ...
 Step 3 : Configure ....

每个步骤或子步骤都可能失败/通过,测试将正常继续。

Step 1 : Verify something     - PASS
Step 2 : Compare ....         - FAIL
 Sub-step 2.1 : Compare ...  - PASS
 Sub-step 2.2 : Compare ...  - FAIL
Step 3 : Configure ....       - PASS

我还需要将结果逐步包含在交互式 HTML 结果文件中(我使用了 Allure),其中包含每个步骤的结果和信息。

我尝试使用 Pytest 和 Allure(用于交互式 HTML 结果报告文件),但为了表示失败,我需要使用 Assert,并且 Assert 异常会导致测试立即失败。

除了 Allure 之外,还有没有其他框架可以帮助我创建高层次的 HTML 报告文件并以 Assert 以外的其他方式表示失败?有没有办法让 Allure 在不使用 Assert 的情况下显示失败?

   def test_procedure_1(self):
        with allure.step("1. Configure "):
           Assert 0
        with allure.step("2. prints"):
           Assert 1
        with allure.step("3. attached"):
           Assert 0
        with allure.step("4. compare"):
           Assert 1

其他例子:

    def test_procedure_2(self):
        with self.subTest("step 1:"):
            Assert 0
            with self.subTest("step 1.1:"):
                Assert 1
                with self.subTest("step 1.1.1:"):
                    Assert 1
                    with self.subTest("step 1.1.1.1:"):
                        Assert 0
        with self.subTest("step 2:"):
             Assert 1

标签: pythonpytestpython-unittestallure

解决方案


推荐阅读