TestNG Example

package com.javarticles.testng;

import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestA {
@BeforeSuite
public void beforeSuite1() {
System.out.println(“TestA: beforeSuite1”);
}

@BeforeSuite
public void beforeSuite2() {
System.out.println(“TestA: beforeSuite2”);
}

@BeforeTest
public void beforeTest(ITestContext testContext) {
System.out.println(“TestA: beforeTest Test<” + testContext.getName() + “>”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“TestA: beforeClass”);
}

@BeforeMethod
public void beforeMethod() {
System.out.println(“TestA: beforeMethod”);
}

@Test
public void unitTest1A() {
System.out.println(“TestA: unitTest1A”);
}

@Test(groups=”feature1″)
public void unitTest2A() {
System.out.println(“TestA: unitTest2A, feature1 test”);
}

@Test
public void unitTest3A() {
System.out.println(“TestA: unitTest3A”);
}

@Test(groups=”feature2″)
public void unitTest4A() {
System.out.println(“TestA: unitTest4A, feature2 test”);
}

@AfterMethod
public void afterMethod() {
System.out.println(“TestA: afterMethod”);
}

@AfterClass
public void afterClass() {
System.out.println(“TestA: afterClass”);
}

@AfterTest
public void afterTest(ITestContext testContext) {
System.out.println(“TestA: afterTest Test<” + testContext.getName() + “>”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“TestA: afterSuite”);
}
}

sampletextng.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”Suite”>
<test name=”UnitTesting”>
<classes>
<class name=”com.javarticles.testng.TestA” />
</classes>
</test>
</suite>

Output:

[TestNG] Running:
C:\javarticles_ws\testNGAnnotations\simpleTestng.xml

TestA: beforeSuite1
TestA: beforeSuite2
TestA: beforeTest Test
TestA: beforeClass
TestA: beforeMethod
TestA: unitTest1A
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest2A, feature1 test
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest3A
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest4A, feature2 test
TestA: afterMethod
TestA: afterClass
TestA: afterTest Test
TestA: afterSuite

===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0

TestB:

package com.javarticles.testng;

import org.testng.ITestContext;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestB {
@BeforeSuite
public void beforeSuite() {
System.out.println(“TestB: beforeSuite”);
}

@BeforeTest
public void beforeTest(ITestContext testContext) {
System.out.println(“TestB: beforeTest Test<” + testContext.getName() + “>”);
}

@BeforeMethod
public void beforeMethod() {
System.out.println(“TestB: beforeMethod”);
}

@Test
public void scenario1B() {
System.out.println(“TestB: Scenario1B”);
}

@Test(groups=”feature1″)
public void scenario2B() {
System.out.println(“TestB: Scenario2B, feature1 test”);
}

@AfterMethod
public void afterMethod() {
System.out.println(“TestB: afterMethod”);
}

@AfterTest
public void afterTest(ITestContext testContext) {
System.out.println(“TestB: afterTest Test<” + testContext.getName() + “>”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“TestB: afterSuite”);
}

@BeforeGroups(groups=”feature1″)
public void beforeGroup() {
System.out.println(“TestB: beforeGroup for feature1″);
}

@AfterGroups(groups=”feature1”)
public void afterGroup() {
System.out.println(“TestB: afterGroup for feature1”);
}
}

TestC:

package com.javarticles.testng;

import org.testng.ITestContext;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestC {
@BeforeSuite
public void beforeSuite() {
System.out.println(“TestC: beforeSuite”);
}

@BeforeTest(alwaysRun=true)
public void beforeTest(ITestContext testContext) {
System.out.println(“TestC: beforeTest Test<” + testContext.getName() + “>”);
}

@BeforeMethod
public void beforeMethod() {
System.out.println(“TestC: beforeMethod”);
}

@Test
public void uiSceario1C() {
System.out.println(“TestC: uiSceario1C”);
}

@Test(groups=”feature1″)
public void uiScenario2C() {
System.out.println(“TestC: uiScenario2C, feature1 test”);
}

@Test(groups=”feature2″)
public void uiScenario3C() {
System.out.println(“TestC: uiScenario3C, feature2 test”);
}

@AfterMethod(groups=”feature1″)
public void afterMethod() {
System.out.println(“TestC: afterMethod”);
}

@AfterTest(alwaysRun=true)
public void afterTest(ITestContext testContext) {
System.out.println(“TestC: afterTest Test<” + testContext.getName() + “>”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“TestC: afterSuite”);
}

@BeforeGroups(groups=”feature2″)
public void beforeGroup() {
System.out.println(“TestC: beforeGroup for feature2″);
}

@AfterGroups(groups=”feature2”)
public void afterGroup() {
System.out.println(“TestC: afterGroup for feature2”);
}
}

Multiple tests:

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”Suite”>
<test name=”UnitTesting”>
<classes>
<class name=”com.javarticles.testng.TestA” />
</classes>
</test>
<test name=”FunctionalTesting”>
<classes>
<class name=”com.javarticles.testng.TestA” />
<class name=”com.javarticles.testng.TestB” />
</classes>
</test>
<test name=”UITesting”>
<classes>
<class name=”com.javarticles.testng.TestC” />
</classes>
</test>
</suite>

Output:

[TestNG] Running:
C:\javarticles_ws\testNGAnnotations\multiTestsTestng.xml

TestA: beforeSuite1
TestA: beforeSuite2
TestB: beforeSuite
TestC: beforeSuite
TestA: beforeTest Test
TestA: beforeClass
TestA: beforeMethod
TestA: unitTest1A
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest2A, feature1 test
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest3A
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest4A, feature2 test
TestA: afterMethod
TestA: afterClass
TestA: afterTest Test
TestA: beforeTest Test
TestB: beforeTest Test
TestA: beforeClass
TestA: beforeMethod
TestA: unitTest1A
TestA: afterMethod
TestB: beforeGroup for feature1
TestA: beforeMethod
TestA: unitTest2A, feature1 test
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest3A
TestA: afterMethod
TestA: beforeMethod
TestA: unitTest4A, feature2 test
TestA: afterMethod
TestA: afterClass
TestB: beforeMethod
TestB: Scenario1B
TestB: afterMethod
TestB: beforeMethod
TestB: Scenario2B, feature1 test
TestB: afterMethod
TestB: afterGroup for feature1
TestA: afterTest Test
TestB: afterTest Test
TestC: beforeTest Test
TestC: beforeMethod
TestC: uiSceario1C
TestC: afterMethod
TestC: beforeMethod
TestC: uiScenario2C, feature1 test
TestC: afterMethod
TestC: beforeGroup for feature2
TestC: beforeMethod
TestC: uiScenario3C, feature2 test
TestC: afterMethod
TestC: afterGroup for feature2
TestC: afterTest Test
TestA: afterSuite
TestB: afterSuite
TestC: afterSuite

===============================================
Suite
Total tests run: 13, Failures: 0, Skips: 0

Run groups:

grouptexting.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”Suite”>
<test name=”Feature1″>
<groups>
<run>
<include name=”feature1″ />
</run>
</groups>
<classes>
<class name=”com.javarticles.testng.TestA” />
<class name=”com.javarticles.testng.TestB” />
<class name=”com.javarticles.testng.TestC” />
</classes>
</test>
<test name=”Feature2″>
<groups>
<run>
<include name=”feature2″ />
</run>
</groups>
<classes>
<class name=”com.javarticles.testng.TestA” />
<class name=”com.javarticles.testng.TestC” />
</classes>
</test>
</suite>

One thing to note when you are running the groups:
The configuration annotated methods won’t run unless you specify the group in the groups attribute or set alwaysRun to true. For example, in our TestC, we have included both the cases.

@AfterMethod(groups=”feature1″)
public void afterMethod() {
System.out.println(“TestC: afterMethod”);
}

@AfterTest(alwaysRun=true)
public void afterTest(ITestContext testContext) {
System.out.println(“TestC: afterTest Test<” + testContext.getName() + “>”);
}

Output:

[TestNG] Running:
C:\javarticles_ws\testNGAnnotations\groupTestng.xml

TestC: beforeTest Test
TestB: beforeGroup for feature1
TestA: unitTest2A, feature1 test
TestB: Scenario2B, feature1 test
TestC: uiScenario2C, feature1 test
TestC: afterMethod
TestB: afterGroup for feature1
TestC: afterTest Test
TestC: beforeTest Test
TestC: beforeGroup for feature2
TestA: unitTest4A, feature2 test
TestC: uiScenario3C, feature2 test
TestC: afterGroup for feature2
TestC: afterTest Test

===============================================
Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

Example of @Parameters

One of the important features of TestNG is data-driven testing.
One can pass parameter values to test methods as arguments following the below methods:

Using testng.xml configuration file.
Through DataProviders
Parameterization is about defining the parameters and its value in testng.xml and then referring to the parameters using @Parameters annotation at method level. The attributes section of @Parameters will have the array of parameters used.
The parameters are defined in testng.xml using the parameter element. Its name and value are specified using the attributes name and value.

parmtesting.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”Suite”>
<parameter name=”param1″ value=”One”></parameter>
<parameter name=”param2″ value=”1″></parameter>
<test name=”ParamTesting”>
<classes>
<class name=”com.javarticles.testng.TestParam” />
</classes>
</test>
</suite>

testparm.java:

package com.javarticles.testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParam {

@Parameters({“param1”, “param2”})
@Test
public void t(String p1, String p2) {
System.out.println(“t(” + p1 + “, ” + p2 + “)” );
}
}

Output:

[TestNG] Running:
C:\javarticles_ws\testNGAnnotations\paramTestng.xml

t(One, 1)

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s