Test ng for testers

26
www.pragmatictestlabs.com/ [email protected] +94 71 873 2025 Next Selenium Training is starting from 30th March 2014 : Batch # : 2014-4

Transcript of Test ng for testers

www.pragmatictestlabs.com/ [email protected] +94 71 873 2025

Next Selenium Training is starting from 30th March 2014 : Batch # : 2014-4

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 TestNGAnnotationsRun Tests in Big Thread PoolsFlexible 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 ProcessWrite 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 Scriptpublic 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

alwaysRunenabled groups

@TestalwaysRundataProviderdataProviderClassdependsOnGroupsdependsOnMethods descriptionenabledinvocationCountinvocationTimeOut singleThreaded, threadPoolSize successPercentage timeOut

Class level annotations

@Test public class Test1 { public void test1() { }

@Test(groups = "g1") public void test2() { } }

ParametersTest 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 TestingData-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

DependanciesInvoking 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 AssertionsAssert.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

Books

Videos : Data Driven Testing

Pragmatic Test Labs

Thank You !

www.pragmatictestlabs.com