How to execute failed test cases using Maven TestNG?


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. There are scenarios where testcases gets failed and user want to re−run it. In such scenarios, maven creates a testng−failed.xml in target folder. The easiest way is to re−run the xml file either from command line or mention in pom.xml after 1st run. Make sure when you are mentioning the xml file in pom.xml, the file should be present otherwise it will throw the error.

In this article we will illustrate how to re−run failed testcases via maven surefire.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTestngClass

  • Step 2: Write 2 @Test method in the class where 2nd test gets failed.

  • Step 3: Now create the testNG.xml as given

  • Step 4: Add suiteXMLFiles tag in pom.xml and mention a variable name as ${xmlFilePath} for testng.xml file path as shown in program section

  • Step 5: Now, run it using command line as mvn test −DxmlFilePath=testng.xml filepath.

  • Step 6: Verify testng−failed.xml is created at target/surefire−reports folder.

  • Step 7: Now re−run this xml file either mentioning into pom.xml or command line.

Example

The following code to show 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(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
	   assert false;
    }   
}  

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>
                <configuration>
                  <suiteXmlFiles>                        <suiteXmlFile>${xmlFilePath}</suiteXmlFile>
                    </suiteXmlFiles> 
                </configuration>
            </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>

Output

[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

Testcase 1 - executed
Testcase 2 - skip exception example
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.314 s <<< FAILURE! - in TestSuite
[ERROR] NewTestngClass.testcase2  Time elapsed: 0.008 s  <<< FAILURE!
java.lang.AssertionError
	at NewTestngClass.testcase2(NewTestngClass.java:16)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   NewTestngClass.testcase2:16
[INFO] 
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[ERROR] There are test failures.

Now, user can see testng−failed.xml file is created at target/surefire−reports folder

User can directly run this file using command:

mvn test -DxmlFilePath=target/surefire-reports/testng-failed.xml

Output

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Testcase 2 - skip exception example
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.095 s <<< FAILURE! - in TestSuite
[ERROR] NewTestngClass.testcase2  Time elapsed: 0.017 s  <<< FAILURE!
java.lang.AssertionError
	at NewTestngClass.testcase2(NewTestngClass.java:16)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   NewTestngClass.testcase2:16
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[ERROR] There are test failures. 

Updated on: 16-Aug-2023

314 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements