TestNG Run Single Test

Profile picture for user arilio666

This article will show how we can run a single test using TestNG.

1.) Create A TestNG Test.

public class TestNG {
  @Test
  public void test1() {
     System.out.println("Test Number 1");
  }
  @Test
  public void test2() {
     System.out.println("Test Number 2");
  }
}
  • Create a class with two test names; for this example, let us run test2.

2.) XML File

Let us create an XML file to run the TestNG tests.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" Sample Test Suite">
<listeners>
<listener class-name="week4.day2.TestNG"/>
</listeners>
<!-- Test -->
 <test name="test2" >
   <classes>
     <class name="week4.day2.TestNG"/>
   </classes>
 </test> 
 <!-- End Test -->
 
</suite> <!-- Suite -->
  • After creating this XML file, pass the TestNG class name in both the listener and class name fields.
  • Then pass the test step test 2 we want to run.
Test Number 2
===============================================
Sample Test Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
  • We can see TestNG has run the second method as we insisted successfully.
Tags