如何解决maven-surefire和tycho-surefire之间的行为不一致,jacoco不生成报告
我正在为项目创建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>
添加到tycho-surefire-plugin上,同时保持打开maven-surefire-plugin会很好。问题甚至是这样,jacoco拒绝创建覆盖站点,并显示以下(非错误)消息:
Skipping JaCoCo execution due to missing execution data file.
我试图寻找两者的解决方案,但是我发现这些解决方案的任何组合都不能使我拥有一个有效的覆盖范围站点。 Maven确实让我很困惑,尤其是周围有鼓风,因此除了实际解决方案之外,我还希望您能对其中的任何解释有所了解。
这是我的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>
解决方法
由于您使用的是非常旧的Surefire版本AspNetCore.Antiforgery
,因此JaCoCo当然不会有任何测试结果。该版本不是为JUnit5创建的。
使用最新版本2.12.4
并查看tutorial。
如果您想拥有很小的POM,请删除依赖项3.0.0-M5
,因为您无需在测试代码中访问JUnit内部。 Surefire会在测试运行之前不久将其下载。
您的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 插件只能在 plugins 部分中使用。问题在于您还在依赖项部分中使用了它。您不想将其放在类路径中。属性 jacoco.agent 的工作是仅将jacoco代理置于测试类中,但是JaCoCo插件必须在Surefire插件之前启动。
我不明白的下一件事是编译器的配置。为什么有这个?
<executions>
<execution>
<id>compiletests</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
关于包装,我还有第二个问题。我从来没有看过这个。这不是标准包装。
<packaging>eclipse-test-plugin</packaging>
Eclipse插件的归档文件是否具有任何特殊的二进制形式?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。