In this post, we will discuss different types of Asserts in the TestNG testing framework. We use asserts to validate the test methods for pass/fail criteria. The assertion tool in the framework will provide assertion methods to validate the actual value and the expected value to mark the test pass/fail in the TestNG test report.
Types of Asserts
Generally, there are three types of Asserts in TestNG
- Hard Asserts
- Soft Asserts
- Custom Asserts
Hard assert fails the test method and throws an AssertionError. Assert( org.testng.Assert ) class in the TestNG framework is an example of a Hard assert type. All the methods in the Assert class are static so that we can use the methods in the class without instantiating the Assert class.
Soft assert, on the other hand, doesn’t throw an exception but just records the failure and continues with the next statement of the test method. Soft Assert is used to overcome the drawback of the hard assert. Soft assert is used when we want to make multiple validations in a test method. We want to make sure all the validations run and to know which assertions have failed.
To know more about SoftAssert:
We can customize the above asserts in the automation and get the hybrid functionality of both the asserts.