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

TestNG

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.

  • onBeforeAssert();
  • onAssertSuccess();
  • onAssertFailure()
  • onAfterAssert();

 

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

 

TestNG Assert Lifecycle

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.

 

 

TestNG Assert Lifecycle

 

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

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

‹ Install TestNG plugin in Eclipse› Hard and Soft dependency in TestNG Framework

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