www.pragmatictestlabs.com/ janesh@pragmatictesters.com +94 71 873 2025
Next Selenium Training is starting from 30th March 2014 : Batch # : 2014-4
TestNG for Testers
- by Pragmatic Testers
Reference
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
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
Configure Maven
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
source : http://testng.org/doc/maven.html
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
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");
}
}
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>
Test Result
Annotations
@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroup
@AfterGroup
@BeforeMethod
@AfterMethod
alwaysRun
enabled
groups
@Test
alwaysRun
dataProvider
dataProviderClass
dependsOnGroups
dependsOnMethods
description
enabled
invocationCount
invocationTimeOut
singleThreaded, threadPoolSize
successPercentage
timeOut
Class level annotations
@Test
public class Test1 {
public void test1() {
}
@Test(groups = "g1")
public void test2() {
}
}
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
Parameters with testing.xml
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
<test name="Simple example">
<-- ... -->
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);
}
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) {
// ...
}
}
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
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>
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
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" />
TestNG Assertions
Assert.assertEquals(Actual, Expected)
Assert.assertEquals(Actual, Expected, Message)
Assert.assertEqualsNoOrder(Actual, Expected)
Assert.assertFalse(Actual)
Assert.assertNotEquals(Actual1, Actual2, Delta)
Assert.assertSame(Actual, Expected)
Assert.fail(Message)
TestNG Reporting
Understanding Java API
Reference
Books
Videos : Data Driven Testing
Tutorials
TestNG Tutorial by MKYong
Testing Tutorials - Search for TestNG tutorial
Tutorials Point
Pragmatic Test Labs
Thank You !
www.pragmatictestlabs.com

Test ng for testers

  • 1.
    www.pragmatictestlabs.com/ janesh@pragmatictesters.com +9471 873 2025 Next Selenium Training is starting from 30th March 2014 : Batch # : 2014-4
  • 2.
    TestNG for Testers -by Pragmatic Testers Reference
  • 3.
    The Creator ofTestNG 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 RunTests 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
  • 5.
  • 6.
    Three Step Process Writethe 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 publicclass 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 TestScript <?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>
  • 9.
  • 10.
  • 11.
  • 12.
    Parameters Test methods canhave 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
  • 13.
    Parameters with testing.xml @Parameters({"first-name" }) @Test public void testSingleString(String firstName) { System.out.println("Invoked testString " + firstName); assert "Cedric".equals(firstName); } <suite name="My suite"> <parameter name="first-name" value="Cedric"/> <test name="Simple example"> <-- ... -->
  • 14.
    Parameters with @DataProvider //Thismethod 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 DifferentClass 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-Driventesting 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 Methodsin 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 TestNGcreates 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" />
  • 20.
    TestNG Assertions Assert.assertEquals(Actual, Expected) Assert.assertEquals(Actual,Expected, Message) Assert.assertEqualsNoOrder(Actual, Expected) Assert.assertFalse(Actual) Assert.assertNotEquals(Actual1, Actual2, Delta) Assert.assertSame(Actual, Expected) Assert.fail(Message)
  • 21.
  • 22.
  • 23.
  • 24.
    Videos : DataDriven Testing
  • 25.
    Tutorials TestNG Tutorial byMKYong Testing Tutorials - Search for TestNG tutorial Tutorials Point
  • 26.
    Pragmatic Test Labs ThankYou ! www.pragmatictestlabs.com