Is there a decent HTML Junit report plugin for Maven?
Asked Answered
L

8

51

I find the surefire-report plug-in very unsuitable to my working style. I clean the project all the time and I don't want to spend 5 min to rebuild the whole site every time I want to look at the test report in my browser.

If I type mvn surefire-report:report-only, the generated report is too ugly and barely readable.

What I'm looking for is something like ant's JUnitReport task. Is there one available out there already?

Lindsy answered 17/5, 2010 at 3:32 Comment(0)
B
31

Indeed, generating the whole site at each build is clearly not an option. But the problem is that mvn surefire-report:report-only doesn't create the the css/*.css files, hence the ugly result. This is logged in SUREFIRE-616 (doesn't mean something will happen though). Personally, I don't use HTML reports that much so I can live with that but that's not a good answer so here is a workaround based on the ant task (*sigh*):

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>test-reports</id>
        <phase>test</phase>
        <configuration>
          <tasks>
            <junitreport todir="target/surefire-reports">
              <fileset dir="target/surefire-reports">
                <include name="**/*.xml"/>
              </fileset>
              <report format="noframes" todir="target/surefire-reports"/>
            </junitreport>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>ant</groupId>
        <artifactId>ant-junit</artifactId>
        <version>1.6.2</version>
      </dependency>
    </dependencies>
  </plugin>

Update: My initial idea was to run the Maven AntRun plugin "on demand" to generate the reports... but that's not what I posted, I bound it to the test phase... But I didn't think about the case of failed tests (that would stop the build and prevent the execution of the AntRun plugin). So, either:

  1. Don't bind the AntRun plugin to the test phase, move the configuration outside the execution and call mvn antrun:run on the command line to generate the reports when wanted.

  2. or use the testFailureIgnore option of the test mojo and set it to true in the surefire plugin configuration:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <testFailureIgnore>true</testFailureIgnore>
      </configuration>
    </plugin>
    
  3. or set this expression from the command line using the -D parameter:

    $ mvn test -Dmaven.test.failure.ignore=true
    

I think that Option #1 is the best option, you don't necessarily want to generate the reports (especially when the test passes) and generate them systematically may slow down the build on the long term. I'd generate them "on demand".

Baccy answered 17/5, 2010 at 8:10 Comment(5)
Does it open a subprocess or just runs ant embedded? If this just embeds ant and it's task, this is exactly what I need.Lindsy
@jimmy It runs the ant task in the same process.Baccy
Take a look at the maven.apache.org/surefire/maven-failsafe-plugin. Build to the 'verify' phase, and it will fail at that point, after the report has been generated.Groundmass
is it possible to change the html report name ? all the html reports are generated with the name as "junit-noframes".Reinke
@PascalThivent Hello, Following this configuration of ANT in MAVEN, Surefire report is generating duplicate tests in the html report. How can I avoid that ?Gibert
R
85

This is what I do:

# Run tests and generate .xml reports
mvn test

# Convert .xml reports into .html report, but without the CSS or images
mvn surefire-report:report-only

# Put the CSS and images where they need to be without the rest of the
# time-consuming stuff
mvn site -DgenerateReports=false

go to target/site/surefire-report.html for the report.

After tests run, the rest of the two run in about 3.5 seconds for me.

Romola answered 30/5, 2014 at 16:7 Comment(1)
This should have been accepted as easiest way to achieve results and the answer.Posology
B
31

Indeed, generating the whole site at each build is clearly not an option. But the problem is that mvn surefire-report:report-only doesn't create the the css/*.css files, hence the ugly result. This is logged in SUREFIRE-616 (doesn't mean something will happen though). Personally, I don't use HTML reports that much so I can live with that but that's not a good answer so here is a workaround based on the ant task (*sigh*):

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>test-reports</id>
        <phase>test</phase>
        <configuration>
          <tasks>
            <junitreport todir="target/surefire-reports">
              <fileset dir="target/surefire-reports">
                <include name="**/*.xml"/>
              </fileset>
              <report format="noframes" todir="target/surefire-reports"/>
            </junitreport>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>ant</groupId>
        <artifactId>ant-junit</artifactId>
        <version>1.6.2</version>
      </dependency>
    </dependencies>
  </plugin>

Update: My initial idea was to run the Maven AntRun plugin "on demand" to generate the reports... but that's not what I posted, I bound it to the test phase... But I didn't think about the case of failed tests (that would stop the build and prevent the execution of the AntRun plugin). So, either:

  1. Don't bind the AntRun plugin to the test phase, move the configuration outside the execution and call mvn antrun:run on the command line to generate the reports when wanted.

  2. or use the testFailureIgnore option of the test mojo and set it to true in the surefire plugin configuration:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <testFailureIgnore>true</testFailureIgnore>
      </configuration>
    </plugin>
    
  3. or set this expression from the command line using the -D parameter:

    $ mvn test -Dmaven.test.failure.ignore=true
    

I think that Option #1 is the best option, you don't necessarily want to generate the reports (especially when the test passes) and generate them systematically may slow down the build on the long term. I'd generate them "on demand".

Baccy answered 17/5, 2010 at 8:10 Comment(5)
Does it open a subprocess or just runs ant embedded? If this just embeds ant and it's task, this is exactly what I need.Lindsy
@jimmy It runs the ant task in the same process.Baccy
Take a look at the maven.apache.org/surefire/maven-failsafe-plugin. Build to the 'verify' phase, and it will fail at that point, after the report has been generated.Groundmass
is it possible to change the html report name ? all the html reports are generated with the name as "junit-noframes".Reinke
@PascalThivent Hello, Following this configuration of ANT in MAVEN, Surefire report is generating duplicate tests in the html report. How can I avoid that ?Gibert
M
6

Create a new maven run configuration and with goal =>

surefire-report:report site -DgenerateReports=false

This can help you to have a better report view with css.

Meldameldoh answered 6/7, 2016 at 12:33 Comment(1)
Thanks! For failsafe: mvn surefire-report:failsafe-report-only site -DgenerateReports=falseBreeder
L
4

Thanks for Pascal, I've found an improved solution to do what I want to do:

<plugin>
    <!-- Extended Maven antrun plugin -->
    <!-- https://maven-antrun-extended-plugin.dev.java.net/ -->
    <groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
    <artifactId>maven-antrun-extended-plugin</artifactId>
    <executions>
      <execution>
        <id>test-reports</id>
        <phase>test</phase>
        <configuration>
          <tasks>
            <junitreport todir="target/surefire-reports">
              <fileset dir="target/surefire-reports">
                <include name="**/*.xml"/>
              </fileset>
              <report format="noframes" todir="target/surefire-reports"/>
            </junitreport>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant-junit</artifactId>
        <version>1.8.0</version>
      </dependency>
      <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant-trax</artifactId>
        <version>1.8.0</version>
      </dependency>
    </dependencies>
  </plugin>

This version uses a newer version of ant and best of all. However, I still haven't found a way to generate a test report when tests fail. How should I do that?

Lindsy answered 19/5, 2010 at 9:10 Comment(2)
Did you get the way to generate report after test failure.?Serow
Hello, Following this configuration of ANT in MAVEN, Surefire report is generating duplicate tests in the html report. How can I avoid that ?Gibert
T
3

You can set -Dmaven.test.failure.ignore=true to generate the test report when tests fail.

Tellez answered 25/6, 2010 at 19:13 Comment(0)
I
3

Here is how I did it using the goal site maven-surefire:report :

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <showSuccess>false</showSuccess>
                    <outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>
Incondensable answered 21/8, 2013 at 22:40 Comment(0)
K
1

Run the below command

mvn clean install surefire-report:report  

You can find the report in the below location

{basedir}/target/site/surefire-report.html

For more details refer the below link

http://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html

Km answered 30/3, 2017 at 12:18 Comment(1)
wich version are you using? because with the 2.22.0 the css and images files are missing when you run that commandCloudland
D
0

Add this pom as above mentioned 1.option take out the configuration from execution phase keep outside, run mvn 'verify' and then run again mvn 'antrun:run'.. Then you are able to see failed test cases as well

<plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>test-reports</id>
                <phase>test</phase>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <tasks>
                <junitreport todir="target/surefire-reports">
                    <fileset dir="target/surefire-reports">
                        <include name="**/*.xml" />
                    </fileset>
                    <report format="noframes" todir="target/surefire-reports" />
                </junitreport>
            </tasks>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>ant</groupId>
                <artifactId>ant-junit</artifactId>
                <version>version</version>
            </dependency>
        </dependencies>
    </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
                  <version>version</version>
            </plugin>
Duarte answered 7/5, 2020 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.