TestNG Tutorial
Introduction to TestNG Configuration and set up of TestNG project Simple TestNG project in IntelliJ IDEA TestNG annotations Assertions in TestNG Difference between assertEquals and assertSame Ignoring the tests in TestNG Grouping the tests timeOut tests in TestNG Exception tests in TestNG dependsOnMethods dependsOnGroups Hard and Soft dependencies Passing parameters using XML file Passing parameters using Data providers Dynamic Data Providers group-by-instances attribute in TestNG XML suite file Test priorities in TestNG invocationCount in TestNG Executing Selenium tests using TestNG Executing TestNG tests using main method Executing JUnit tests from within TestNG Running tests in Parallel in TestNG Executing only failed tests in TestNG TestNG integration with Maven TestNG integration with Gradle Reporter class in TestNG TestNG Reports Execution Listener Test Listener Comparison between JUnit and TestNGExecution listener in testng
We might need to do some set up before starting the execution of entire test suite or after finishing the execution of entire suite. Execution listeners can be used in this scenario. With the help of listeners, we can invoke some methods right before and after suite execution. First of all we need to create one class that implement IExecutionListener and then we need to pass the listener in Suite XML file. Here is the sample class that implements the IExecutionListener interface. In this interface, we have 2 methods – onExecutionStart and onExecutionFinish. So we need to define these methods in class that implements IExecutionListener interface as depicted in below example.
package org.softpost;
import org.testng.IExecutionListener;
public class ExecutionListener implements IExecutionListener {
public void onExecutionStart() {
System.out.println("This method will be called right before starting the execution");
}
public void onExecutionFinish() {
System.out.println("This method will be called right after execution is finished");
}
}
Here is the sample TestNG.XML file. Please note that we have passed the listener class in "listeners" tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite">
<listeners>
<listener class-name="org.softpost.ExecutionListener" />
</listeners>
<test name="Tests">
<classes>
<class name="org.softpost.Class1" />
</classes>
</test>
</suite>
Here is the output of above code.
This method will be called right before starting the execution
[TestNG] Running:
C:UsersSagarIdeaProjectsTestNG-Projectsrc est
esources estng.xml
Test1 from Class1
Test2 from Class1
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
This method will be called right after execution is finished
Web development and Automation testing
solutions delivered!!