3. The Creator of TestNG
Cédric Beust is a French software engineer and a software
technology author. He is the co-author of two books and the
creator of the TestNG Java testing framework
In 2004, he created TestNG an open source Java testing framework that has seen a lot
of adoption, especially in the web testing area.
He is still actively working on the framework on his spare time to support the community. I
maintain both the TestNG core and its Eclipse plug-in
4. Features of TestNG
Annotations
Run Tests in Big Thread Pools
Flexible Test Configuration
Support for Data Driven Testing
Support for Parameters
Powerful Execution Model - (TestSuite)
Supported by variety of tools and Plugins
source : http://testng.org/doc/index.html
6. Three Step Process
Write the test script and insert TestNG annotations
Add the information about your test in a testng.xml file
(e.g. the class name, the groups you wish to run, etc.)
Run TestNG
7. First Test Script
public class SimpleTest {
@BeforeClass
public void beforeClass() {
// code that will be invoked when this test is instantiated
}
@Test(groups = { "fast" })
public void aFastTest() {
System.out.println("This is a fast test");
}
@Test(groups = { "slow" })
public void aSlowTest() {
System.out.println("This is a slow test");
}
}
8. Invoke the Test Script
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Test1">
<groups>
<run>
<include name="fast"/>
</run>
</groups>
<classes>
<class name="selenium.testng.example.SimpleTest"/>
</classes>
</test>
</suite>
12. Parameters
Test methods can have parameters
Can use any number of parameters in Test methods
Pass parameters using @Parameters
From testing.xml : For simple values
With DataProviders : For complex parameters
14. Parameters with @DataProvider
//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
15. DataProvider in Different Class
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { new Integer(42) }
}
}
}
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
}
16. Data Driven Testing
Data-Driven testing generally means executing a set of steps with
multiple sets of data.
Selenium does not provide any out-of-the box solution for data driven
testing but leaves it up to the user to implement this on his own.
TestNG is a framework that makes data-driven testing possible in
selenium. TestNG is a testing framework created in line with Junit but
with added features that makes it suitable for use in regression test
automation projects.
source : http://functionaltestautomation.blogspot.com/2009/10/dataprovider-data-driven-testing-with.html
17. Dependancies
Invoking Test Methods in certain order
With Annotations
Hard Dependencies : Must run and succeed
Soft Dependencies : Add alwaysRun=”true” to @Test
With XML
<test name="My suite">
<groups>
<dependencies>
<group name="c" depends-on="a b" />
<group name="z" depends-on="c" />
</dependencies>
</groups>
</test>
18. Running Failed Tests
TestNG creates a testng-failed.xml in output directory
Contains failed methods
Allows to re-run the failed tests
Can reproduce the failures and verify fixes quickly
19. TestNG and Selenium
@BeforeSuite(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) {
String driverPropertyName = context.getCurrentXmlTest().getParameter("driver.property.name");
String driverPath = context.getCurrentXmlTest().getParameter("driver.path");
String browserType = context.getCurrentXmlTest().getParameter("browser.type");
String baseURL = context.getCurrentXmlTest().getParameter("base.url");
if (browserType == null) {
driver = new HtmlUnitDriver();
} else if (browserType.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
} else if (browserType.equalsIgnoreCase("Chrome")) {
System.setProperty(driverPropertyName,driverPath);
driver = new ChromeDriver();
} //For other browsers
logger.info("BROWSER_TYPE=" + BROWSER_TYPE);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(TIME_OUT, TimeUnit.SECONDS);
}
<parameter name="browser.type" value="Chrome" />