Site icon TestingDocs.com

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()

 

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

Exit mobile version