source:http://www.javarticles.com/2015/04/testng-annotations.html
TestNG annotations:
@BeforeSuite
will be executed before any tests declared inside a TestNG suite.@BeforeTest
will be executed before each test section declared inside a TestNG suite.@BeforeClass
will be executed before any of the test methods of a test class.@BeforeMethod
will be executed before the execution of each test method.@BeforeGroups
will run before any of the test method of the specified group is executed.groups
attribute must contain the list of groups this method belongs to.@AfterSuite
will be executed after any tests declared inside a TestNG suite.@AfterTest
will be executed after each test section declared inside a TestNG suite.@AfterClass
will be executed after the last test method of a test class.@AfterMethod
will be executed after the execution of each test method.@AfterGroups
will run after the last test method of the specified group is executed.groups
attribute must contain the list of groups this method belongs to.@DataProvider
method provides the data for a test method and must return a two dimensional object array (Object[ ][ ]) as data.@Factory
method returns an array of class objects (Object[ ]). This is used to run a set of test cases with different values provided to the test class during its instantiation.@Test
marks a class or a method as a test method. If used at class level, all the public methods of a class will be considered as a test method.@Listeners
is defined at class level to specify an array of test listeners classes extendingorg.testng.ITestNGListener
.@Parameters
is used to pass parameters to a test method. These parameter values are provided in thetestng.xml
configuration file at runtime.
xml structure
- suite
- groups
- /groups
- test
- classes
- class
- /class
- /classes
- classes
- /test
- /suite
Annotation firing order
-
@BeforeSuite
methods -
@BeforeTest
methods -
@BeforeClass
methods -
@BeforeMethod
methods <-- for each @Test method -
@Test
methods <-- for each @Test method -
@AfterMethod
methods <-- for each @Test method -
@AfterClass
methods -
@AfterTest
methods -
@AfterSuite
methods
Using Parameters in the testng.xml file
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
suite
name
=
"Suite"
>
<
parameter
name
=
"param1"
value
=
"PARAM1"
></
parameter
>
<
parameter
name
=
"param2"
value
=
"PARAM2"
></
parameter
>
<
test
name
=
"ParametersTest"
>
<
classes
>
<
class
name
=
"com.uc.testng.ParamTest"
/>
</
classes
>
</
test
>
</
suite
>
package
com.uc.testng;
import
org.testng.annotations.Parameters;
import
org.testng.annotations.Test;
public
class
ParamTest {
@Parameters
({
"param1"
,
"param2"
})
@Test
public
void
pTest(String p1, String p2) {
System.out.println(
"pTest("
+ p1 +
", "
+ p2 +
")"
);
}
}
[TestNG] Running:
paramTestng.xml
pTest(PARAM1, PARAM2)
Listeners
Listeners are a flexible method to manage test & suite events
ITestListener
public class CustomListeners extends TestBase implements ITestListener
package com.uc.listeners; import java.io.IOException; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.SkipException; import com.relevantcodes.extentreports.LogStatus; import com.uc.base.TestBase; import com.uc.utilities.TestUtil; //adding TestBase features public class CustomListeners extends TestBase implements ITestListener { @Override public void onTestStart(ITestResult arg0) { test = rep.startTest(arg0.getName().toUpperCase()); //runmode - Y if(!TestUtil.isTestRunnable(arg0.getName(), excel)) { // TestNG logging ( extentReports logging will be in the onTestSkipped() method throw new SkipException("Skipping the test "+ arg0.getName().toUpperCase()+" as the Runmode is N"); } } @Override public void onTestSuccess(ITestResult arg0) { //get the passed test case name from TestBase.test for the message TestBase.test.log(LogStatus.PASS, arg0.getName().toUpperCase() +": PASS"); // end the test rep.endTest(test); //flush the reports to disk rep.flush(); } @Override public void onTestFailure(ITestResult arg0) { // for hyperlinks in testng reports System.setProperty("org.uncommons.reportng.escape-output","false"); TestUtil.captureScreenshot(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //on failure log the failure to extent, then pass on the throwed exception test.log(LogStatus.FAIL, arg0.getName().toUpperCase()+" Failed with exception : " + arg0.getThrowable()); test.log(LogStatus.FAIL, test.addScreenCapture(TestUtil.screenshotName)); rep.endTest(test); rep.flush(); //Show image link & image Reporter.log("Capturing Screenshot"); Reporter.log("<a target=\"_blank\" href=\"" + TestUtil.screenshotName +"\">Screenshot</a><br/>"); Reporter.log("<a href=\""+TestUtil.screenshotName+"\"><img height=\"50%\" width=\"50%\" src=\""+TestUtil.screenshotName +"\"/></a>"); } @Override public void onTestSkipped(ITestResult arg0) { test.log(LogStatus.SKIP, arg0.getName().toUpperCase() + " : Skipped the test as the Runmode is N"); rep.endTest(test); rep.flush(); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // TODO Auto-generated method stub } @Override public void onStart(ITestContext context) { // TODO Auto-generated method stub } @Override public void onFinish(ITestContext context) { // TODO Auto-generated method stub } }
ISuiteListener
package com.uc.listeners; import org.testng.ISuite; import org.testng.ISuiteListener; import com.uc.base.TestBase; public class CustomSuiteListeners extends TestBase implements ISuiteListener{ @Override public void onStart(ISuite suite) { // TODO Auto-generated method stub } @Override public void onFinish(ISuite suite) { // TODO Auto-generated method stub } }
- Log in to post comments