TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

SoftAssert in TestNG Framework

Introduction

In this post, we will discuss SoftAssert in the TestNG framework. Sometimes, we might write more than one assert in a @Test method and want to make all the asserts run even if one or more fails. This can be achieved using SoftAssert in TestNG.

Using SoftAssert when an assertion fails, it doesn’t throw an exception but records the failure. Invoking the .assertAll() method, will cause an exception to be thrown if at least one assertion fails.

 

Hard assert vs soft assert

Hard Assert

package com.testingdocs.testng.tutorial;

import org.testng.Assert;
import org.testng.annotations.Test;

public class HardAssertExample { 
 @Test 
 public void AssertTest() {
 Assert.assertEquals("testing","testingDocs.com", "Assert One"); 
 Assert.assertEquals("testingDoc.com","testingDocs.com",
 "Assert Two");
 Assert.assertFalse(true, "Assert Three"); 
 Assert.assertEquals(2,3, "Assert Four"); 
 } 
}

 

Hard Assert

 

Hard Assert vs Soft Assert

Let’s assume we are testing a webpage with 4 issues that fail. If we use normal static Assert methods the test would fail with an assertion error on the first issue. SoftAssert allows all assertions to run, no matter if they pass or fail. Also, if there are failures, we may want to see all the issues in the test.

Before using soft assert we will need to instantiate it, like as shown below:

SoftAssert sa = new SoftAssert();

SoftAssert in TestNG

Sample Soft Assert Example

package com.testingdocs.selenium.demo;

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

public class SoftAssertExample
{
 SoftAssert sa = new SoftAssert();
 
 @Test
 public void softAssertTest() 
 {
 sa.assertEquals("testing","testingDocs.com", "Soft Assert One");
 sa.assertEquals("testingDoc.com","testingDocs.com", "Soft Assert Two");
 sa.assertFalse(true, "Soft Assert Three");
 sa.assertEquals(2,3, "Soft Assert Four");
 sa.assertAll();
 }

}

 

Run output

FAILED: softAssertTest
java.lang.AssertionError: The following asserts failed:
Soft Assert One expected [testingDocs.com] but found [testing],
Soft Assert Three expected [false] but found [true],
Soft Assert Four expected [3] but found [2]

Note that we can see that all the assertions have run and failed assertions are reported.

 

SoftAssert TestNG

 

The difference between hard assert and soft assert is that soft assert allows all the asserts to be executed. Therefore, all of them will be run, even if they fail, but after all, are run, the results are displayed. These results include all the failures that were encountered during the test execution. As usual, if there is at least one failure, the test will be marked as failed as shown in the above example.

SoftAssert in TestNG Framework

 

Conclusion

In the above test example, the sa.assertAll() method collects all the failures and decides whether to fail the test or not at the end. Also, if you forget to add the .assertAll(), the test will pass, and no failures will be reported.

 

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ Custom Assertions in TestNG Framework› How to Take Screenshot when an Assert fails

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version