1

我正在为一个项目创建一个 pom 并向其中添加测试用例。该项目是一个eclipse插件。

用 tycho 编译项目工作得很好,唯一的问题是在测试期间:如果我同时运行 maven-surefire-plugin 测试和 tycho-surefire-plugin-tests,前者按预期执行所有测试,而后者给出以下错误:

Execution test of goal org.eclipse.tycho:tycho-surefire-plugin:1.7.0:test failed: Tycho build extension not configured for MavenProject

<skipTests>true</skipTests>在保持 maven-surefire-plugin 开启的同时添加到 tycho-surefire-plugin 会非常好;问题就是这样,jacoco 拒绝创建覆盖站点,并带有以下(非错误)消息:

Skipping JaCoCo execution due to missing execution data file.

我试图寻找两者的解决方案,但我发现的解决方案的任何组合都不会让我拥有一个有效的覆盖站点。Maven 真的让我很困惑,尤其是在 tycho 的情况下,所以我会在实际修复的基础上做出任何解释。

这是我的pom:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
    
    <groupId>mygroupid</groupId>
    <artifactId>myartifactid</artifactId>
    <name>myname</name>
    <packaging>eclipse-test-plugin</packaging>
    
    <properties>
        <tycho-version>1.7.0</tycho-version>
    </properties>

    <parent>
        <groupId>parentgroupid</groupId>
        <artifactId>parent</artifactId>
        <version>0.9.5</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
        </dependency>
        
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
    </dependencies>

    <build>
        <testSourceDirectory>src/test/java/</testSourceDirectory>
        <plugins>
            

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                
                <configuration>
                </configuration>

                <executions>
                    <execution>
                        <id>test</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>**/Test_*.java</include>
                            </includes>
                        </configuration>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-surefire-plugin</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
                <executions>
                    <execution>
                        <id>test</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>**/Test_*.java</include>
                            </includes>
                        </configuration>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <configuration>
                    <output>file</output>
                    <append>true</append>
                    <includes>
                        <include>**/path_to_source/**/*</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
         
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>compiletests</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这是我的父母pom:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>parentgroupid</groupId>
    <artifactId>parent</artifactId>
    <version>0.9.5</version>
    <packaging>pom</packaging>
    <modules>
        <module>moduleid</module>
    </modules>

    <properties>
        <tycho-version>1.7.0</tycho-version>
    </properties>

    <repositories>
     <repository>
         <id>eclipse-2020-06</id>
         <layout>p2</layout>
         <url>http://download.eclipse.org/releases/2020-06</url>
     </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>${tycho-version}</version>
                <extensions>true</extensions>
                
                <configuration>
                    <includeAllDependencies>true</includeAllDependencies>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
4

2 回答 2

1

当然,由于您使用的是非常旧的 Surefire 版本,因此 JaCoCo 不会有任何测试结果2.12.4。此版本不是为 JUnit5 创建的。使用最新版本3.0.0-M5并查看教程

如果您想拥有微型 POM,请删除依赖项,junit-jupiter-engine因为您不需要访问测试代码中的 JUnit 内部。Surefire 将在测试运行前不久下载它。

于 2020-10-05T01:18:28.513 回答
1

您的 POM 有几个错误。让我们从根本原因开始,然后从高到低其他优先级。

整个问题是 Surefire 不知道 JaCoCo。你必须以这种方式给“他”打电话(见jacoco.agent),这两者都“连线”。请阅读 JaCoCo 项目中的文档:

<properties>
  <jvm.args.tests>-Xmx2048m -Xms1024m -XX:SoftRefLRUPolicyMSPerMB=50 -Djava.awt.headless=true -Djdk.net.URLClassPath.disableClassPathURLCheck=true</jvm.args.tests>
<properties>
...
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>${jvm.args.tests} ${jacoco.agent}</argLine>
      </configuration>
...

下一个错误与您使用插件的方式有关。插件jacoco-maven-plugin只能在插件部分使用。问题是您也在依赖项部分使用它。您不想将它放在类路径中。属性jacoco.agent的工作是将 jacoco 代理仅放在测试 classpth 上,但 JaCoCo 插件必须在 Surefire 插件之前启动。

接下来我不明白的是编译器的配置。为什么你有这个?

             <executions>
                <execution>
                    <id>compiletests</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>

我有第二个关于包装的问题。我从来没有见过这个。它不是标准包装。

<packaging>eclipse-test-plugin</packaging>

Eclipse 插件是否有任何特殊的二进制形式的存档文件?

于 2020-10-10T18:09:29.160 回答