0% found this document useful (0 votes)
50 views

Testng: Case1 ( (Priority 1) Tcase1 (System. .Println ) (Priority 0) Tcase2

TestNG is a testing framework for Java that supports unit testing and provides annotations to control test flow and organization. Key features include: 1. Controlling test execution order using annotations like @BeforeTest and @AfterTest as well as priority levels. 2. Configuring tests via an XML file to select classes, methods, groups to include or exclude. 3. Grouping tests and selectively running groups using the testng.xml file. 4. A defined order of execution for annotations from @BeforeSuite to @AfterSuite.

Uploaded by

Bhupinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Testng: Case1 ( (Priority 1) Tcase1 (System. .Println ) (Priority 0) Tcase2

TestNG is a testing framework for Java that supports unit testing and provides annotations to control test flow and organization. Key features include: 1. Controlling test execution order using annotations like @BeforeTest and @AfterTest as well as priority levels. 2. Configuring tests via an XML file to select classes, methods, groups to include or exclude. 3. Grouping tests and selectively running groups using the testng.xml file. 4. A defined order of execution for annotations from @BeforeSuite to @AfterSuite.

Uploaded by

Bhupinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TESTNG

Unit Testing tool for Java program, Same as Junit.

Annotations: meta tag which gives information about the method placed next to it

Order of Annotation : -

@BeforeSuite

@ BeforeTest

@BeforeGroup

@BeforeClass

@BeforeMethod

@Test

@afterMethod

@AfterClass

@Aftergroup

@AfterTest

@AfterSuite

1 Control Execution order of different test in TestNg

For this in front of test annotations we can set priority, lower the number means higher the priority

@test(priority=1)

@test(priority=2)

public class Case1 {

@Test(priority =1)
public void TCase1()
{
System.out.println("This is Case1");
}
@Test(priority =0)
public void TCase2()
{
System.out.println("This is Case2");
}

2 Create Testng.xml

 First we should have suite tag in that


<suite name="MyAppName>

</suite>

 Inside Suite tag, we should have test tag


<suite name="MyAppName>
<test name="Test1">
</test>
</suite>

 Inside test, we must defile all classes that belongs to particular test

<suite name="MyAppName">
<test name="Test1">
<classes>
<class name="org.nitin.Case1"/>
</classes>
</test>
</suite>

Note : A basic set of xml is ready, which can be run, here we can execute whatever the number
of cases we want to execute

3 Update textng.xml in such a way that I want to include only few @test out of all existing @tests in
class

 Inside class, add <methods> tag, inside method, use <include> tag, in include pass name of test
<suite name="MyAppName">
<test name="Test1">
<classes>
<class name="org.nitin.Case1">
<methods>
<include name="TCase2"/>
</methods>
</class>
</classes>

</test>
</suite>

*** Same way we can use exclude test, by which we can decide which @test, we
want to exclude

4 Grouping of Test and Execute only group that we want to execute

 First add groups in front of tests


@test(groups={“group1”,”group2”})

public class Case1 {

@Test(groups={"Smoke","Sanity"})
public void TCase1()
{
System.out.println("This is Case1");
}
@Test(groups={"sanity"})
public void TCase2()
{
System.out.println("This is Case2");
}

 Now in testng.xml, create a groups tag inside test tag, inside group
create a run, inside run, use include and give name of test
 <suite name="MyAppName">
 <test name="Test1">
 <classes>
 <class name="org.nitin.Case1">
 </class>
 </classes>
 <groups >
 <run>
 <include name="Smoke"/>
 </run>
 </groups>
 </test>
 </suite>

5 Execution order of different annotations

package org.nitin;

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 Case1 {


@BeforeSuite
public void BSuite()
{
System.out.println("Before Suite");
}
@AfterSuite
public void ASuite()
{
System.out.println("After Suite");
}
@BeforeTest
public void BTest()
{
System.out.println("Before Test");
}
@AfterTest
public void ATest()
{
System.out.println("Before Test");
}
@BeforeClass
public void BClass()
{
System.out.println("Before Class");
}
@AfterClass
public void AClass()
{
System.out.println("After Class");
}
@Test(groups={"Smoke","Sanity"})
public void TCase1()
{
System.out.println("This is Case1");
}
@Test(groups={"sanity"})
public void TCase2()
{
System.out.println("This is Case2");
}
@BeforeMethod
public void BMethod()
{
System.out.println("Before method");
}
@AfterMethod
public void AMethod()
{
System.out.println("After method");
}

Testng.xml

<suite name="MyAppName">
<test name="Test1">
<classes>
<class name="org.nitin.Case1">
</class>
</classes>
<groups>
<run>
<include name="Smoke"/>
</run>
</groups>
</test>
</suite>

You might also like