Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jenkins TestNG Failed Test Cases for Maven

DZone's Guide to

Jenkins TestNG Failed Test Cases for Maven

A former Jenkins newbie provides some clarification on a common issue that other Jenkins initiates might face using TestNG and WebDriver.

· Performance Zone
Free Resource

As I switched from a software testing company to another, I was introduced to Jenkins for the first time. Being a newbie to the tool, I got stuck on a point where I would run the command: 

cd path to workspace
mvn test -DtestNG.file=/target/surefire-reports/testng-failed.xml


Here, it would run testng.xml instead of running testng-failed.xml. Now I often find new testers struggling through this same dilemma. Here’s what happened:

Once I configured it on my Windows desktop, I used TestNG and Selenium Webdriver for writing test cases to run failed test cases twice before considering that they are failing. Moving forward, I created a job in Jenkins which mapped to my workspace and run the testng.xml.

I specified the path of testng.xml in pom.xml. and created a maven project and used the pom.xml for running test cases. Once I figured out that I was getting a successful run of all test cases, I was able to achieve surefire reports of the tests.

Unfortunately, when I ran the above command, it would automatically run testng.xml instead of running the expected testng-failed.xml. I was not sure if I was following the correct path for running failed test cases.

So, I approached a senior of the software testing company I work with now and thankfully he was able to provide me with a solution. I also found out that this is a common scenario software testers come across when they are introduced to Jenkins initially. This is what was going wrong:

The failed-testng.xml were getting deleted whenever I would re-run the tests in Post task under Jenkins. This was simply because when you run "mvn clean," it automatically deletes the target folder where the failed-testng.xml files were present. You will have to access the Jenkins workspace if you want to be assured, but it may be a case that your software testing company may not have the access to it.

You can resolve the issue by following these steps:

First your pom should be configurable to run the tests from command line:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-surefire-plugin</artifactId>

    <version>2.19.1</version>

    <configuration>

      <forkCount>0</forkCount>

      <suiteXmlFiles>

        <suiteXmlFile>src/test/resources/${suiteXmlFile}</suiteXmlFile>

      </suiteXmlFiles>

    </configuration>

  </plugin>


Copy or move the failed-testng.xml from the target folder to any other location within the framework, except target and test-output folder. This is simply because these folders will be deleted when you run the command "mvn clean".

Now, configure Jenkins for the Post build to run the failed-testng.xml.

Here is the Maven plugin to move file from one location to another:

<plugin>

    <artifactId>maven-resources-plugin</artifactId>

    <version>3.0.1</version>

    <executions>

      <execution>

        <id>copy-resources</id>

        <!-- here the phase you need -->

        <phase>validate</phase>

        <goals>

          <goal>copy-resources</goal>

        </goals>

        <configuration>

          <outputDirectory>${basedir}/target/failed-testng.xml</outputDirectory>

          <resources>         

            <resource>

              <directory>src/</directory>

              <filtering>true</filtering>

            </resource>

          </resources>             

        </configuration>           

      </execution>

</executions>

  </plugin>


Note: This plugin must be executed after the tests execution.

Topics:
jenkins ,testing attributes ,software testing ,maven

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}