单个软断言错误使后续通过的方案失败

时间:2019-11-28 06:48:42

标签: selenium-webdriver testng assertion

我在测试类中使用软断言,并从数据提供者获取测试数据。下面是示例代码:

@Test(dataProvider = "userPages")
public void validatePages(String pageName) {
    try {
        Reporter.log("Logged In User : " + userType, true);
        //Page1 takes extra checks
        if (pageName.equalsIgnoreCase("Page1")) {
            System.out.println("Running in Page1 bloc ");
            softAssert.assertEquals(page.isValidPage1(), true,
                    "Failed to loada : " + pageName);
        } else {
            System.out.println("Running in NON Page1 bloc .. Page Name is :: "+pageName);
            softAssert.assertEquals(page.isValid(), true,
                    "Failed to load : " + pageName);
        }
        softAssert.assertAll();  ---> Appends page1 failures to all the subsequent passed page tests thus marking all of them failed.
    } catch (InterruptedException e) {
        Reporter.log("Test failed for : " + pageName);
    }
    //
}

TestNG 依赖项是::

 <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
  </dependency>

在我的情况下,如果单个页面失败,那么即使所有后续页面都通过了,也都标记为失败。我尝试调试该问题,发现在 SoftAssert 类的 assertAll()方法中,以前失败的测试数据的错误消息被附加了

public void assertAll() {
if (!m_errors.isEmpty()) {
  StringBuilder sb = new StringBuilder("The following asserts failed:");
  boolean first = true;
  for (Map.Entry<AssertionError, IAssert<?>> ae : m_errors.entrySet()) {
    if (first) {
      first = false;
    } else {
      sb.append(",");
    }
    sb.append("\n\t");
    sb.append(ae.getKey().getMessage());
  }
  throw new AssertionError(sb.toString());
}}

此问题标志着testNG报告中的错误错误。请提供帮助和建议。

Edit1:

数据提供者

@DataProvider(name = "userPageNames")
public Object[] creatingObjects() {
    Map<String, String> pagesList = null;
    try {
        pagesList = //Populate Map

    } catch (Exception e) {
        Reporter.log("Failed to get Pages List for " + userType, true);
    }

    Object[] object = new Object[pagesList.keySet().size()];
    Integer i = 0;
    for (String pageName : pagesList.keySet()) {
        object[i] = pageName;
        i++;
    }
    return object;
}

报告:真正的失败

Logged In User : user1
Running in Page1 bloc ..
Page verified : Page1

java.lang.AssertionError: The following asserts failed:
Failed to loada : Page1 expected [true] but found [false]
Expected :true
Actual   :false



    at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
    at web.dishaPages.ValidatePagesTestNew.validatePages(ValidatePagesTestNew.java:88)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:230)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:175)
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:242)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:576)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

报告:虚假失败

Logged In User : user1
Running in NON Page1 bloc .. Page Name is :: ViewDMPage
Page verified : ViewDMPage

java.lang.AssertionError: The following asserts failed:
Failed to loada : Page1 expected [true] but found [false]
Expected :true
Actual   :false



    at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
    at web.dishaPages.ValidatePagesTestNew.validatePages(ValidatePagesTestNew.java:88)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:230)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:175)
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:242)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:576)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

1 个答案:

答案 0 :(得分:0)

问题出在您的测试代码上。

由数据提供者提供动力的@Test方法基本上是使用一个SoftAssert实例,然后使用同一实例为所有数据提供者数据集迭代调用assertAll()

SoftAssert旨在记住迄今为止通过调用assertXXX记录的所有断言。

要解决此问题,您应该在SoftAssert方法内实例化@Test对象