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

TestNG

Custom Assertions in TestNG Framework

Introduction

If we write tests using the TestNG framework perhaps we use the Assert class. But, most of the methods in this class are static, and the constructor is protected. So, in this post, we will explore how to write our own custom assertions to get more handle on the operations.

We can extend the Assertion class to perform some hooks on the assert life-cycle methods. We would like to perform a certain operation whenever an assertion passes or fails etc. Furthermore, we can override the methods in Assertion according to our own needs.

Assert LifeCycle

Assertion implements IAssertLifecycle, which defines several life cycle methods that subclasses can override. Also, some are listed below:

1. onBeforeAssert()
2. onAssertSuccess()
3. onAssertFailure()
4. onAfterAssert()

 

AssertLifeCycle

Custom Assertion:

Lets write a sample custom class which extends the Assertion .

public class CustomAssertion extends Assertion {
 
 private List<String> assert_messages = Lists.newArrayList();
 
 @Override
 public void onBeforeAssert(IAssert a) {
 assert_messages.add("BeforeAssert:" + a.getMessage());
 }
 
 @Override
 public void onAfterAssert(IAssert a) {
 assert_messages.add("AfterAssert:" + a.getMessage());
 }
 
 @Override
 public void onAssertSuccess(IAssert<?> assertCommand) {
 assert_messages.add("OnlyOnAssertSuccess:" + assertCommand.getMessage());
 }
 
 @Override
 public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {
 assert_messages.add("OnlyOnAssertFailure:" + assertCommand.getMessage());
 
 }
 
 public List<String> getAssertMessages() {
 return assert_messages;
 }
}

Now to demonstrate the usage of custom assert operations ,
we will write a sample TestNG test class.

public class AssertionExample {
 
 private CustomAssertion m_custom = new CustomAssertion();
 
 @Test
 public void sampletest1() {
 m_custom.assertTrue(true, "Assert in sampletest1");
 }
 
 @Test
 public void sampletest2() {
 m_custom.assertTrue(false, "Assert in sampletest2");
 }
 
 @AfterClass
 public void print() {
 System.out.println("Print Assert messages:" );
 System.out.println(m_custom.getAssertMessages());
 }

}

 

class=”lang:java decode:true”>Run output of the program:
Print Assert messages:
[BeforeAssert:Assert in  sampletest1, OnlyOnAssertSuccess:Assert in  sampletest1, AfterAssert:Assert in  sampletest1, BeforeAssert:Assert in sampletest2, OnlyOnAssertFailure:Assert in sampletest2, AfterAssert:Assert in sampletest2]
PASSED: sampletest1
FAILED: sampletest2

java.lang.AssertionError: Assert in sampletest2 expected [true] but found [false]

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

 

 

In the above example, we are just collection the assert messages. We can make use of the above idea to extend custom logging when ever an assert fails. Let’s say we want to capture screenshot in webdriver tests when an assert fails.

In conclusion, we can continue to use Assert class in TestNG, but extending Assertion in our tests to gain additional flexibility is a good idea.

 

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

‹ TestNG Test Priority› SoftAssert in TestNG Framework

Recent Posts

  • Update draw.io on Windows
  • 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