Site icon TestingDocs.com

TestNG Assert Lifecycle

Overview

First things first, let’s have a look at the Assert lifecycle in TestNG. We can extend the Assertion class to perform some hooks on the assert lifecycle methods. Note that certain operations are performed when an Assertion passes/fails. Furthermore, we can override the methods in Assertion according to our own needs in the testing framework.

IAssertLifecycle

IAssertLifecycle is an interface with the methods for the assertion classes to implement. Typical it runs the assert commands that are passed in the parameter to

executeAssert() method.

The interface declaration in the TestNG framework:

public interface IAssertLifecycle {

...

}

TestNG Assert Lifecycle

Let’s take a quick look at the other methods. Assertion implements IAssertLifecycle, which defines several life cycle methods that sub classes can override and specify implementations to the following methods.

 

Some of the capabilities that we can perform by overriding the life cycle methods:

 

For example, onAssertSuccess() method is invoked when an assert succeeds in the test method. On the other hand, onAssertFailure() method is invoked when an assert fails in the test methods.

The method signatures of the assert lifecycle accepts an assert command as a parameter.

void onAssertSuccess(IAssert<?> assertCommand);

For example, override the onAssertFailure() method in the Assert Lifecycle. We can override or enhance this method to capture screenshots whenever an Assert fails.

 

 

 

A sample test method to make the Assert fail and capture the screenshot when the assert fails.

https://www.testingdocs.com/how-to-take-screen-shot-with-testng-when-an-assert-fails/

 

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

Exit mobile version