How to run a TestNG class without using testng.xml with maven?


TestNG is a testing framework and can use Maven as build tool. It helps to maintain dependencies and their version at one place in pom.xml

Maven provides flexibility to run using surefire plugin. It allows user to run testng.xml as well as directly run a testng class without using a testng.xml. There are few pre−requisites to achieve this:

  • All testng classes should be created under src/test/java. If classes are not created under these directories user should have to pass testng.xml in pom.xml file.

  • By default, maven surefire plugin identifies following classes:

    • "**/Test*.java" − includes all of its subdirectories and all Java filenames that start with "Test".

    • "**/*Test.java" − includes all of its subdirectories and all Java filenames that end with "Test".

    • "**/*Tests.java" − includes all of its subdirectories and all Java filenames that end with "Tests".

    • "**/*TestCase.java" − includes all of its subdirectories and all Java filenames that end with "TestCase".

In this article we will illustrate how to run a TestNG class with maven without using testng.xml.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTes under src/test/java

  • Step 2: Write 2 @Test method in the class.

  • Step 3: Make sure suiteXMLFiles tag in removed from pom.xml.

  • Step 4: Now, run it using command line as mvn test. It will run all classes that fulfil above condition in project.

Example

The following code to show how to run only 1 test method from a large suite:

src/test/java/NewTest.java

import org.testng.annotations.Test;

public class NewTest {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
    }
   @Test(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTest");
	   
    }

    
}  

pom.xml

This is a maven configuration file that is used to organize dependencies, plugins and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>         
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

Command to Run specific suite

mvn test 

Output

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running newTest
in test case 1 of NewTest 
in test case 2 of NewTest 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.73 s - in newTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.916 s
[INFO] Finished at: 2022-02-10T08:32:07+05:30
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0 

Updated on: 17-Aug-2023

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements