Created
December 4, 2016 02:46
-
-
Save gururajhm/455124dfc6fb13c69fd95ee9819cefc9 to your computer and use it in GitHub Desktop.
Example of ISuiteListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<String, String> parms = new HashMap<String, String>(); | |
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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output : | |
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected. | |
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" 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