46

我觉得这个surefire-report插件非常不适合我的工作风格。我一直在清理项目,我不想每次想在浏览器中查看测试报告时都花费 5 分钟来重建整个站点。

如果我键入mvn surefire-report:report-only,生成的报告太丑陋且难以阅读。

我正在寻找的是类似于 ant 的 JUnitReport 任务。那里已经有可用的了吗?

4

8 回答 8

81

这就是我所做的:

# 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

前往 target/site/surefire-report.html 获取报告。

测试运行后,其余两个对我来说运行大约 3.5 秒。

希望有帮助。享受!

于 2014-05-30T16:07:06.350 回答
31

事实上,在每次构建时生成整个站点显然不是一种选择。但问题是它mvn surefire-report:report-only不会创建 css/*.css 文件,因此结果很丑。这是在SUREFIRE-616中记录的(但这并不意味着会发生某些事情)。就个人而言,我不太使用 HTML 报告,所以我可以忍受,但这不是一个好的答案,所以这里有一个基于 ant 任务 (*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>

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

  1. 不要将 AntRun 插件绑定到test阶段,将配置移到阶段之外execution并在需要时调用mvn antrun:run命令行以生成报告。

  2. 或者使用testFailureIgnoretest mojo 的选项并在 surefire 插件配置中将其设置为 true:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <testFailureIgnore>true</testFailureIgnore>
      </configuration>
    </plugin>
    
  3. 或使用 -D 参数从命令行设置此表达式:

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

我认为选项 #1 是最好的选择,您不一定要生成报告(尤其是在测试通过时)并系统地生成它们可能会长期减慢构建速度。我会“按需”生成它们。

于 2010-05-17T08:10:55.273 回答
4

感谢 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,最重要的是。但是,我仍然没有找到在测试失败时生成测试报告的方法。我该怎么做?

于 2010-05-19T09:10:13.943 回答
4

创建一个新的 Maven 运行配置和目标 =>

surefire-report:report site -DgenerateReports=false

这可以帮助您使用 css 获得更好的报表视图。

于 2016-07-06T12:33:43.260 回答
3

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

于 2010-06-25T19:13:07.450 回答
2

这是我使用目标站点 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>
于 2013-08-21T22:40:21.800 回答
0

运行以下命令

mvn clean install surefire-report:report  

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

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

有关更多详细信息,请参阅以下链接

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

于 2017-03-30T12:18:49.463 回答
0

如上所述添加这个 pom 1.option 从执行阶段取出配置保留在外面,运行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>
于 2020-05-07T07:51:15.507 回答