Site icon TestingDocs.com

Maven Surefire Plugin Jenkins

Introduction

When Maven runs tests in a project, it automatically generates XML based test reports in a directory called surefire-reports in the target under the project directory. To install the Maven surefire plugin we can the plugin information to the Maven pom.xml file.

Sample file with Surefire

<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testingdocs</groupId>
<artifactId>TestNGTutorials</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-edge-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

The plugin creates test reports when we run the Maven test goal. By default, the reports are created under the target folder in the surefire-reports directory.

 

 

Sample Triangle Tests

package com.triangle.demo;

public class Triangle {
        
        public static final int NOTATRIANGLE = -1;
        public static final int SCALENE = 0;
        public static final int ISOSELES = 1;
        public static final int EQUILATERAL = 2;
        
        public static int check(int a, int b, int c) {

                boolean isTriangle;

                if((a < b + c) && (b < a + c) && (c < a + b))
                        isTriangle = true;
                else
                        isTriangle = false;

                int triangleType = NOTATRIANGLE;
                if(isTriangle) {
                        if((a == b) && (b == c))
                                triangleType = EQUILATERAL;
                        else if((a != b) && (a != c) && (b != c))
                                triangleType = SCALENE;
                        else
                                triangleType = ISOSELES;
                } else
                        triangleType = NOTATRIANGLE;
                
                return triangleType;
        }

}

Triangle Class Tests

package com.triangle.demo;

import static org.junit.Assert.*;
import org.junit.Test;

public class TriangleTest {
        @Test
        public void testEquilateralTriangle() {
              assertEquals(Triangle.EQUILATERAL,
                                Triangle.check(7, 7, 7));
        }
        
        @Test
        public void testIsoselesTriangle() {
              assertEquals(Triangle.ISOSELES,
                                Triangle.check(4, 4, 6));
        }
        
        @Test
        public void testScaleneTriangle() {
                assertEquals(Triangle.SCALENE,
                                Triangle.check(6, 7, 9));
        }
}

Let’s add some sample tests to a Maven project and run them using Jenkins build.

Console Output

Build the project and click on the Console output. You can see that Jenkins reports your tests.

[INFO]
[INFO] — maven-surefire-plugin:2.22.1:test (default-test) @ triangle-demo —

[INFO]
[INFO] ——————————————————-
[INFO] T E S T S
[INFO] ——————————————————-

[INFO] Running com.triangle.demo.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.289 s – in com.triangle.demo.AppTest
[INFO] Running com.triangle.demo.TriangleTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s – in com.triangle.demo.TriangleTest

[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[JENKINS] Recording test results

————————-

 

To view the test result, click on the build and Test Report link in the sidebar as shown below.

 

You can click on the package link and drill down the test results of individual tests.

 

Sample report format

Sample surefire report index.html in the surefire-reports folder

 

 

More Maven Tutorials on this website can be found at:
https://www.testingdocs.com/apache-maven-tutorial/

 

Exit mobile version