Test ng for testers
Upcoming SlideShare
Loading in...5

Like this? Share it with your network

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 3

http://www.slideee.com 3

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. www.pragmatictestlabs.com/ janesh@pragmatictesters.com +94 71 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 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
  • 5. 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
  • 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>
  • 9. Test Result
  • 10. Annotations @BeforeSuite @AfterSuite @BeforeTest @AfterTest @BeforeGroup @AfterGroup @BeforeMethod @AfterMethod alwaysRun enabled groups
  • 11. @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() { } }
  • 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
  • 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 //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" />
  • 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. TestNG Reporting
  • 22. Understanding Java API Reference
  • 23. Books
  • 24. Videos : Data Driven Testing
  • 25. Tutorials TestNG Tutorial by MKYong Testing Tutorials - Search for TestNG tutorial Tutorials Point
  • 26. Pragmatic Test Labs Thank You ! www.pragmatictestlabs.com