单个软断言错误使后续通过的场景失败
single soft assertion error is failing subsequent passed scenarios
我在测试中使用软断言 class 并从数据提供程序获取测试数据。下面是示例代码:
@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 class 的 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 报告中标记错误。请帮助和建议。
编辑 1:
数据提供者
@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.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.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)
问题出在你的测试代码上
您的数据提供者支持的 @Test
方法基本上是使用一个 SoftAssert
实例,然后使用同一实例为所有数据提供者数据集迭代调用 assertAll()
。
SoftAssert
旨在记住迄今为止通过调用 assertXXX
记录的所有断言。
要解决此问题,您应该在 @Test
方法
中实例化 SoftAssert
对象
我在测试中使用软断言 class 并从数据提供程序获取测试数据。下面是示例代码:
@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 class 的 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 报告中标记错误。请帮助和建议。
编辑 1:
数据提供者
@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.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.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)
问题出在你的测试代码上
您的数据提供者支持的 @Test
方法基本上是使用一个 SoftAssert
实例,然后使用同一实例为所有数据提供者数据集迭代调用 assertAll()
。
SoftAssert
旨在记住迄今为止通过调用 assertXXX
记录的所有断言。
要解决此问题,您应该在 @Test
方法
SoftAssert
对象