Saturday 14 March 2020

TestNG: Soft Assertions

In my previous post, I explained about hard assertions. In testNG, if the hard assertion fails, then the statements after the assertion in given test case will not get executed.

In case of soft assertions, these do not throw an error when the assertion fails, it continue with the next statements.

How to create soft assertion?
Using SoftAssertion class, you can create soft assertions.
@Test
public void test2() {
 SoftAssert softAssert = new SoftAssert();

 System.out.println("Failing the assertion");
 softAssert.assertTrue(false);
 System.out.println("I will get executed");

}

SoftAssertTest.java
package com.sample.app.assertions;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertTest {

 @Test
 public void test1() {
  System.out.println("test1 in execution");
 }

 @Test
 public void test2() {
  SoftAssert softAssert = new SoftAssert();

  System.out.println("Failing the assertion");
  softAssert.assertTrue(false);
  System.out.println("I will get executed");

 }

 @Test
 public void test3() {
  System.out.println("test3 in execution");
 }

}

Run SoftAssertTest.java, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
test1 in execution
Failing the assertion
I will get executed
test3 in execution
PASSED: test1
PASSED: test2
PASSED: test3

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
As you see the console messages, all the test cases are passed, but ideally test2 should fail. What are we missing here? We need to assert all the soft assertions at the end of test case.

softAssert.assertAll();

SoftAssertTest.java
package com.sample.app.assertions;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertTest {

 @Test
 public void test1() {
  System.out.println("test1 in execution");
 }

 @Test
 public void test2() {
  SoftAssert softAssert = new SoftAssert();

  System.out.println("Failing the assertion");
  softAssert.assertTrue(false);
  System.out.println("I will get executed");

  softAssert.assertAll();
 }

 @Test
 public void test3() {
  System.out.println("test3 in execution");
 }

}

Run SoftAssertTest.java, you will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
test1 in execution
Failing the assertion
I will get executed
test3 in execution
PASSED: test1
PASSED: test3
FAILED: test2
java.lang.AssertionError: The following asserts failed:
 did not expect to find [true] but found [false]
 at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:47)
 at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:31)
 at com.sample.app.assertions.SoftAssertTest.test2(SoftAssertTest.java:21)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
===============================================


Previous                                                    Next                                                    Home

No comments:

Post a Comment