java 有没有一个像样的HTML Junit报告插件的Maven?

pgccezyw  于 2022-12-25  发布在  Java
关注(0)|答案(8)|浏览(118)

我发现surefire-report插件非常不适合我的工作风格,我一直在清理项目,我不想每次我想在浏览器中查看测试报告时都花5分钟来重建整个网站。
如果我输入mvn surefire-report:report-only,生成的报告太难看,几乎无法阅读。
我正在寻找类似ant的JUnitReport任务的东西,是否已经有一个可用的任务了?

nafvub8i

nafvub8i1#

我是这么做的:

# 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

请转到目标/站点/surefire-report. html查看报告。
测试运行后,其余两个运行在大约3.5秒。

wz3gfoph

wz3gfoph2#

实际上,在每次构建时生成整个站点显然不是一个选项,但问题是mvn surefire-report:report-only不会创建css/.css文件,因此结果很难看。(但不代表会发生什么)。我不太使用HTML报告,所以我可以接受,但这不是一个好答案,所以这里是一个基于蚂蚁任务的变通方案( 叹气 *):

<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>

**更新:**我最初的想法是“按需”运行Maven AntRun插件来生成报告...但这不是我发布的,我将其绑定到test阶段...但我没有考虑测试失败的情况(这将停止构建并阻止AntRun插件的执行)。

1.不要将AntRun插件绑定到test阶段,将配置移到execution之外,并在需要时在命令行上调用mvn antrun:run来生成报告。
1.或者使用test mojo的testFailureIgnore选项并在surefire插件配置中将其设置为true:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <testFailureIgnore>true</testFailureIgnore>
  </configuration>
</plugin>

1.或使用-D参数从命令行设置此表达式:

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

我认为选项#1是最好的选择,你不一定要生成报告(特别是当测试通过时),系统地生成它们可能会拖慢长期的构建。我会“按需”生成它们。

atmip9wb

atmip9wb3#

创建新的maven运行配置,目标=〉

surefire-report:report site -DgenerateReports=false

这可以帮助你有一个更好的报表视图与css。

7lrncoxx

7lrncoxx4#

感谢Pascal,我找到了一个改进的解决方案来做我想做的事情:

<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>

这个版本使用了一个新版本的ant,最好的是,我仍然没有找到一种方法来生成测试报告,当测试失败时,我应该怎么做呢?

5lhxktic

5lhxktic5#

下面是我如何使用目标***站点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>
h43kikqp

h43kikqp6#

您可以设置-Dmaven.test.failure.ignore=true以在测试失败时生成测试报告。

z0qdvdin

z0qdvdin7#

运行以下命令

mvn clean install surefire-report:report

您可以在以下位置找到该报告

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

有关详细信息,请参阅以下链接
http://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html

mpgws1up

mpgws1up8#

如上所述添加此pom 1.选项从执行阶段取出配置保持在外部,运行mvn 'verify'然后再次运行mvn 'antrun:run'..然后您也可以看到失败的测试用例

<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>

相关问题