【问题标题】:Allure report logged only the first fail and the test ends and doesn't run all steps after the first failAllure 报告仅记录第一次失败并且测试结束并且在第一次失败后不运行所有步骤
【发布时间】:2019-06-04 15:55:25
【问题描述】:

我正在使用 Java+TestNG+Allure。我需要在 Allure 报告中获取所有测试失败,不仅是测试的第一次失败,而且全部失败,尽管步骤失败,测试应该从头到尾运行。

【问题讨论】:

标签: report allure


【解决方案1】:

为了在 Allure 报告中报告测试失败,我们必须在 Allure Class 中进行一些修改。在这里,我们想将任何子步骤报告为失败,执行其余步骤,然后将主测试步骤标记为失败测试。为此,我们可以使用 SoftAssertions 的概念。我创建了一个名为 AllureLogger 的类。在类中,我们将有 5 个方法。
1)starttest() 2)endtest() 3) markStepAsPassed(String message) 4)marstepAsFailed(String message) 5)logStep().

    public class AllureLogger {
        public static Logger log = Logger.getLogger("devpinoylog");
        private static StepResult result_fail;
        private static StepResult result_pass;
        private static String uuid;
        private static SoftAssert softAssertion;


        public static void startTest() {
            softAssertion = new SoftAssert();
        }

        public static void logStep(String discription) {
            log.info(discription);
            uuid = UUID.randomUUID().toString();
            result_fail = new StepResult().withName(discription).withStatus(Status.FAILED);
            result_pass = new StepResult().withName(discription).withStatus(Status.PASSED);
        }

        public static void markStepAsFailed(WebDriver driver, String errorMessage) {
            log.fatal(errorMessage);
            Allure.getLifecycle().startStep(uuid, result_fail);
            Allure.getLifecycle().addAttachment(errorMessage, "image", "JPEG", ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
            Allure.getLifecycle().stopStep(uuid);
            softAssertion.fail(errorMessage);
        }


        public static void markStepAsPassed(WebDriver driver, String message) {
            log.info(message);
            Allure.getLifecycle().startStep(uuid, result_pass);
            Allure.getLifecycle().stopStep(uuid);
        }

        public static void endTest() {
            softAssertion.assertAll();
            softAssertion = null;
            startTest();
            softAssertion = new SoftAssert();
        }

    }

在上面的类中,我们使用了与 allureClass 不同的方法,并且我们正在做一些修改以添加软断言。

每次我们在 testClass 中启动一个 TestMethod 时,我们都可以调用 starttest() 和 end testmethod()。在测试方法内部,如果我们有一些子步骤,我们可以使用 try catch 块将子步骤标记为通过或失败。例如,请检查以下测试方法作为示例

    @Test(description = "Login to application and navigate to Applications tab ")
        public void testLogin() 
        {
            AllureLogger.startTest();
            userLogin();
            navigatetoapplicationsTab();
            AllureLogger.endTest();
        }

上面是一个测试方法,它将登录到一个应用程序,然后导航到应用程序选项卡。在里面我们有两个方法将被报告为子步骤,1)login()- 用于登录应用程序 2) navigatetoapplicationsTab()-导航到应用程序选项卡。如果任何子步骤失败,则主步骤和子步骤将被标记为失败,其余步骤将被执行。

我们将在测试方法中定义的上述函数的主体定义如下:

userLogin()
{
  AllureLogger.logStep("Login to the application");
  try
  {
    /*
 Write the logic here
    */
AllureLogger.MarStepAsPassed(driver,"Login successful");
  }
  catch(Exception e)
{
AllureLogger.MarStepAsFailed(driver,"Login not successful");
}
}


navigatetoapplicationsTab()
{
  AllureLogger.logStep("Navigate to application Tab");
  try
  {
    /*
 Write the logic here
    */
AllureLogger.MarStepAsPassed(driver,"Navigate to application Tab successful");
  }
  catch(Exception e)
{
e.printStackTrace();
AllureLogger.MarStepAsFailed(driver,"Navigate to application Tab failed");
}
}

每次抛出任何异常时,它们都会被捕获到 catch 块中并在 Allure Report 中报告。软断言使我们能够成功执行所有剩余的步骤。

附件是使用上述技术生成的Allure报告的截图。主要步骤标记为失败,其余测试用例已执行。

这里所附的报告不是来自上面提到的例子。这只是报告外观的一个示例。

【讨论】:

  • 当它到达这一行时 softAssertion.assertAll();它会引发以下异常 java.lang.ClassCastException:java.lang.AssertionError 无法转换为 java.lang.Exception。请问有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 2012-08-14
  • 1970-01-01
  • 2017-03-31
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多