【问题标题】:Within Maven : how to display detailed SureFire test results (convert from xml to html)?在 Maven 中:如何显示详细的 SureFire 测试结果(从 xml 转换为 html)?
【发布时间】:2018-05-10 01:19:09
【问题描述】:

如何让 Maven/SureFire 生成 HTML 测试报告而不是 XML 报告? 适用于 SureFire、PMD 和其他质量控制工具。

所以它看起来像here

【问题讨论】:

  • 我认为使用文档可以为您指明正确的方向 - maven.apache.org/surefire/maven-surefire-report-plugin/…。还是您遇到了其他问题?
  • 谢谢 Nick,是否有通用 Maven 插件可以将 XML 报告(来自其他插件)转换为 HTML?原帖中的链接以 HTML 格式显示所有报告,即 PMD、Sonar、CheckStyle FindBugs
  • 嗯,我不确定我是否完全理解您的需求。您是否正在寻找一个可以将任何旧的 xml 报告转换为 html 的插件?包括surefire报告插件可能不支持的报告(例如代码覆盖率)?
  • 是的,就是这样
  • 据我了解,surefire 报告插件只会显示它在您列出的项目报告链接中列出的内容,而不是您向其抛出的“任何”xml 报告。但我认为该插件可以满足您的需求,除非您需要支持特定类型的报告。

标签: maven pmd maven-surefire-plugin


【解决方案1】:

根据我们上面的对话,听起来您正在寻找一种多合一的 XML -> HTML 转换。我不知道有这样的插件,但我相信与其他一两个插件结合使用的安全报告插件会让你到达你需要的地方。

首先,我会查看usage docs for the surefire-report plugin。此外,我还整理了一个快速示例,说明如何结合使用 surefire、pmd 和 surefire 报告插件:

使用以下项目结构:

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── Main.java
    │   │   └── SomeClass.java
    │   └── resources
    └── test
        └── java
            └── SampleTest.java

还有这些类定义:

SomeClass.java

import java.util.List; //Unused import, so PMD will have something to pick up

public class SomeClass {

    public void testMethod(){
        System.out.println("This is my test method with some PMD violations");
    }
}

SampleTest.java

import org.junit.Assert;
import org.junit.Test;

//Sample tests, so the surefire report will have something to show
public class SampleTest {

    @Test
    public void test1(){
        Assert.assertTrue(true);
    }

    @Test
    public void test2(){
        Assert.assertTrue(false);
    }
}

最后是 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sample</groupId>
    <artifactId>plugins</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.9.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.21.0</version>
            </plugin>
        </plugins>
    </reporting>
</project>

结果是当我运行时

mvn clean pmd:pmd site

然后我可以打开生成的站点:

open target/site/index.html

从那里,我可以看到我生成的报告,在我的例子中,只有 PMD 和 surefire 测试报告:

此外,如果您想获得更多可能无法使用surefire 报告插件的报告,请查看Jacoco,甚至可以查看Sonar。

【讨论】:

  • 谢谢尼克,一个非常详细而优雅的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2019-11-17
相关资源
最近更新 更多