Main Tutorials

TestNG – Configuration Annotations Example

testng-configuration

In TestNG, we can use the following annotations to do the configuration for your test class, like setup / clean a database, preparing the dummy data, deploy / shut down the server and etc.


@BeforeSuite - For suite test, run before all tests in this suite have run. 
@AfterSuite -  For suite test, run after all tests in this suite have run.

@BeforeTest - For suite test, run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest - For suite test, run after all the test methods belonging to the classes inside the <test> tag have run. 

@BeforeGroups: Run before the first test method that belongs to the group is invoked. 
@AfterGroups: Run after the last test method that belongs to the groups is invoked. 

@BeforeClass - Run before the first test method in the current class is invoked. 
@AfterClass - Run after all the test methods in the current class have been run. 

@BeforeMethod - Run before each test method. 
@AfterMethod - Run after each test method.

P.S Suite test – Run multiple test classes together.

Review the following examples to see the execution order – which method is called first, and which is next.

1. Single Test Class

Running a single test case, show the use of before/after group, class and method.

TestConfiguration.java

package com.mkyong.testng.examples.configuration;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestConfiguration {
	
	@BeforeGroups("shopping")
	public void beforeGroups() {
		System.out.println("@BeforeGroups");
	}

	@AfterGroups("shopping")
	public void afterGroups() {
		System.out.println("@AfterGroups");
	}

	@BeforeClass
	public void beforeClass() {
		System.out.println("@BeforeClass");
	}

	@AfterClass
	public void afterClass() {
		System.out.println("@AfterClass");
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("@BeforeMethod");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("@AfterMethod");
	}

	@Test(groups = "shopping")
	public void runTest1() {
		System.out.println("@Test - runTest1");
	}

	@Test
	public void runTest2() {
		System.out.println("@Test - runTest2");
	}
}

Output



@BeforeClass 

@BeforeGroups 
@BeforeMethod 
@Test - runTest1
@AfterMethod  
@AfterGroups  

@BeforeMethod
@Test - runTest2
@AfterMethod

@AfterClass

PASSED: runTest1
PASSED: runTest2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

2. Suite Test Classes

Create 2 more test classes to show the use of before/after suite and test.

DBConfig.java

package com.mkyong.testng.examples.configuration;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class DBConfig {

	@BeforeSuite()
	public void beforeSuite() {
		System.out.println("@BeforeSuite");
	}

	@AfterSuite()
	public void afterSuite() {
		System.out.println("@AfterSuite");
	}

	@BeforeTest()
	public void beforeTest() {
		System.out.println("@BeforeTest");
	}

	@AfterTest()
	public void afterTest() {
		System.out.println("@AfterTest");
	}

}
TestDBConnection.java

package com.mkyong.testng.examples.configuration;

import org.testng.annotations.Test;

public class TestDBConnection {

	@Test
	public void runOtherTest1() {
		System.out.println("@Test - runOtherTest1");
	}

	@Test
	public void runOtherTest2() {
		System.out.println("@Test - runOtherTest2");
	}

}

Create an XML file to run multiple test cases together. Read the XML comments for self-explanatory.

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<!-- @BeforeSuite -->
<suite name="TestAll">

	<!-- @BeforeTest -->
	<test name="case1">
	  <classes>
		<class name="com.mkyong.testng.examples.configuration.TestConfiguration" />
		<class name="com.mkyong.testng.examples.configuration.TestDBConnection" />
		<class name="com.mkyong.testng.examples.configuration.DBConfig" />
	  </classes>
	</test>
	<!-- @AfterTest -->
	
	<!-- @BeforeTest -->
	<test name="case2">
	  <classes>
		<class name="com.mkyong.testng.examples.configuration.TestDBConnection" />
		<class name="com.mkyong.testng.examples.configuration.DBConfig" />
	  </classes>
	</test>
	<!-- @AfterTest -->
	
</suite>
<!-- @AfterSuite -->

Output



@BeforeSuite

@BeforeTest		//Start {case1}
@BeforeClass
@BeforeGroups
@BeforeMethod
@Test - runTest1
@AfterMethod
@AfterGroups
@BeforeMethod
@Test - runTest2
@AfterMethod
@AfterClass
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest		//End {case1}


@BeforeTest		//Start {case2}
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest		//End {case2}

@AfterSuite

===============================================
TestAll
Total tests run: 6, Failures: 0, Skips: 0
===============================================

Done.

References

  1. TestNG Documentation
  2. TestNG – Run Multiple Test Classes (Suite Test)

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
14 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Daxi
9 years ago

Thanks for posting such a nice and neat tutorial!

Parimal
9 years ago

I am not able to figure out the output of 2. Suite test cases. You are using XML to run the test cases. In the test classes, @Beforeclass is not used anywhere but it does appear in the output of the test case. Why is it so?

I tried to figure out the output and according to my understanding, it should be as shown below:

@BeforeSuite

@BeforeTest

@Test – runOtherTest1

@AfterTest

@BeforeTest

@Test – runOtherTest2

@AfterTest

@AfterSuite

Please let me know if this is correct. If not, please let me know what is wrong with my approach and what is the correct approach.

Thank you.

Giridhar
10 years ago

Please can anybody help me. How to use Mockito with TestNG ???

Rino
10 years ago

I am new to testing. I’ve copied the above code as such and getting errors of “Type mismatch:cannot convert from Test to Annotation”. Is there any jars that I must add?

Thanks in adwance

Nastassia
9 years ago
Reply to  Rino

Considering how long ago the question was asked, you already know the answer; but for future visitors – one should check if the class’ name is “Test” and change it to some other name 🙂

Kavitha
11 years ago

Hi,

I have 3 test

@BeforeTest

@AfterTest

@Test – to Login

@Test – to create Employee

@Test – to logout.

When i execute this second test(Create Employee) is getting executed first and then login and test to create employee and logout is throwing an error.

please let me know what has to be done..

Syed Yunus
7 years ago
Reply to  Kavitha

Prioritize your tests…

@BeforeTest

@AfterTest

@Test(priority=01) – to Login

@Test(priority=02) – to create Employee

@Test(priority=03) – to logout.

Kumar Gowda
10 years ago
Reply to  Kavitha

TESTNG executes the @Test Method in alphabetical order so toCreate is called first then to Login

Gerrit Leder
11 years ago
Reply to  Kavitha

As far as I know JUnit, tests are executed in random order except for the setup and teardown methods. Therefore put login into setup (@BeforeTest) and logout into teardown (@AfterTest)!

Majid
12 years ago

Hello mkyong,

I have one query.

I have TestNG Framework integrated into the eclipse.

I wanted to execute a java package(contains multiple java files) and obtain the induvidual results in the html report (Default_test.html)

My question is that i have the testng.xml file configured with the name of the package to be executed, but i dont have the idea of how to write a corresponding java file that calls this package, excecutes it and generate the results(In default_test.html) of all the java files present in that package.

Below is the sample code snippet of testng.xml file to configure a package.

Could you please help me out in this?

Thanks in advance.

Thanks & Regards,
Majid M A

Antonina
11 years ago
Reply to  Majid

Majid,
you can create dependency between tests:
https://mkyong.com/unittest/testng-tutorial-7-dependency-test/

moi
13 years ago

How to start your class ?

Giridhar
10 years ago
Reply to  mkyong

how to use Mockito with TestNG????