- 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 execute a single test from a large testing suite using TestNG.xml?
testNG.xml is very flexible and it can work as a harness file to execute the test cases. It keeps the development and execution separate from each other. A user can develop "N" number of test cases in testNG but can run a limited number of test methods based on the configuration in testNG.xml.
In this article, let's see how to run only one test method from a large TestNG suite.
To run only one test method, we will use the 'include' keyword from TestNG. In testNG.xml, first we will define the class name where the method is present and after that mention the test name which is going to execute along with the 'include' keyword.
Approach/Algorithm to solve this problem −
Step 1 − import org.testng.annotations.* for TestNG in a class.
Step 2 − Write an annotation as @test
Step 3 − Create a method for the @test annotation as test1.
Step 4 − Repeat the steps for test2 and test3.
Step 5 − Similarly, create "n" number of classes and add multiple tests. In this example, we will create 2 classes with each class having 2 test methods.
Step 6 − Now create the testNG.xml as given below.
Step 7 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
The following code shows how to run only 1 test method from a large suite −
src/ NewTestngClass.java
import org.testng.annotations.Test; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test 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 public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test 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"> <classes> <class name = "NewTestngClass"> <methods> <include name="testCase1" /> </methods> </class> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass =============================================== Suite1 Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
- Related Articles
- How to exclude a test class from a testing suite using testng.xml?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to execute a selected test from a collection of tests in Pytest?
- How to retrieve all test methods name in TestNG suite?
- How to test and execute a regular expression in JavaScript?
- How to use single statement suite with Loops in Python?
- How to force end an entire test suite from the BeforeSuite annotation if a condition is met in TestNG?
- How to retrieve the test suite name at runtime in TestNG?
- How to execute zombie and orphan process in a single C program?
- How to execute a particular test method multiple times (say 5 times) in\nTestNG?
- How to execute a Javascript using Selenium in Python?
- How to encrypt a large file using openssl?
- How to Test a Transistor Using a Digital Multimeter
- How to execute a JavaScript function using its name in a variable?
- e-Commerce Testing: How to Test an e-Commerce Website?
