- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run test groups in TestNG?
Group test is a new innovative feature in TestNG, which is not available in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods.
Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding other sets.
Group tests provide maximum flexibility in how you partition your tests. You do not have to recompile anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.
Now, let's take an example to see how group test works.
Approach/Algorithm to solve this problem −
Step 1 − Create two TestNG classes − NewTestngClass and OrderofTestExecutionInTestNG.
Step 2 − Write two different @Test methods in both of the classes: NewTestngClass and OrderofTestExecutionInTestNG.
Step 3 − Mark each @Test with group as mentioned below in the code file.
Step 4 − Now create the testNG.xml as given below to execute @Test those are having group name as functest.
Step 5 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Step 6 − As per the given configuration, it will execute the Test Case 1 of NewTestngClass and both the test cases of OrderofTestExecutionInTestNG class.
Example
The following code shows how to run testing groups −
src/ NewTestngClass.java
import org.testng.annotations.Test; public class NewTestngClass { @Test(groups = { "functest", "checkintest" }) public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test(groups = {"checkintest" }) public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } }
src/OrderofTestExecutionInTestNG.java −
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test(groups = { "functest" }) public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(groups = { "functest", "checkintest" }) public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <groups> <run> <include name = "functest" /> </run> </groups> <classes> <class name = "NewTestngClass" /> <class name = "OrderofTestExecutionInTestNG" /> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How to run multiple test classes in TestNG?
- How to combine multiple groups to single Test in TestNG?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to run TestNG from command line?
- How to get the name of a test method that was run in a TestNG teardown method?
- How will you run a prerequisite method and post condition method for\nevery test in TestNG?
- How to skip TestNG test at runtime?
- How to skip a particular test method in TestNG?
- How to disable an entire unit test in TestNG?
- How to set priority to the test cases in TestNG?
- How to retrieve all test methods name in TestNG suite?
- How to run Selenium WebDriver test cases in Chrome?
- How to overlook a particular test method from execution in TestNG?
- How to retrieve the test suite name at runtime in TestNG?
- How to pass a variable from BeforeTest to Test annotation in TestNG?
