Skip to content

Instantly share code, notes, and snippets.

@gururajhm
Created December 4, 2016 02:46
Show Gist options
  • Save gururajhm/455124dfc6fb13c69fd95ee9819cefc9 to your computer and use it in GitHub Desktop.
Save gururajhm/455124dfc6fb13c69fd95ee9819cefc9 to your computer and use it in GitHub Desktop.
Example of ISuiteListener
suiteListenerTestng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="SuiteListenerExample">
<listeners>
<listener class-name="com.IsuiteListener.SuiteListener" />
</listeners>
<suite-files>
<suite-file path="./childSuite.xml"/>
</suite-files>
</suite>
childSuite.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Child Suite">
<test name="Test">
<classes>
<class name="com.IsuiteListener.SuiteListenerExample"/>
</classes>
</test>
</suite>
SuiteListenerExample is the test class. Its @BeforeSuite method depends on parameter "testing"
package com.IsuiteListener;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class SuiteListenerExample {
@Parameters("testing")
@BeforeSuite
public void beforeSuite(String parm) {
System.out.println("before suite, testing value: " + parm);
}
@Test
public void testcase1() {
System.out.println("simple test method");
}
@AfterSuite
public void afterSuite() {
System.out.println("after suite");
}
}
In SuiteListener.onStart, we set the parameter testing to value "Regression".
SuiteListener
package com.IsuiteListener;
import java.util.HashMap;
import java.util.Map;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.xml.XmlSuite;
public class SuiteListener implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
System.out.println("Start suite " + suite.getName());
XmlSuite xmlSuite = suite.getXmlSuite();
if (!xmlSuite.getTests().isEmpty()) {
Map&lt;String, String&gt; parms = new HashMap&lt;String, String&gt;();
parms.put("testing", "regression");
System.out.println("Set testing param value");
xmlSuite.setParameters(parms);
}
}
@Override
public void onFinish(ISuite suite) {
System.out.println("Finish suite " + suite.getName());
}
}
The SuiteListener fires once for the child suite and then the parent suite.
Output :
[TestNGContentHandler] [WARN] It is strongly recommended to add "&lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt;" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNGContentHandler] [WARN] It is strongly recommended to add "&lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt;" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] Running:
Start suite Child Suite
Set testing param value
before suite, testing value: regression
simple test method
after suite
Finish suite Child Suite
===============================================
Child Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Running:
Start suite SuiteListenerExample
Finish suite SuiteListenerExample
===============================================
SuiteListenerExample
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment