如何确保某些测试在 Maven 构建中执行而不是被默默忽略?
我最近被迫切换到 Groovy 3。因此,我也更新了我的 Spock 版本。不幸的是,我错过了 Spock 2 需要 JUnit 5 的事实。我只有 JUnit 4 和 Spock 测试,因此 Maven Surefire 插件使用 JUnit 4 提供程序来执行测试。所有 Spock 测试都被忽略了。我只是偶然注意到这一点。
我正在寻找一种方法来检查以 *Spec 结尾的某些(或任何)测试是否在执行的测试中。我查看了 Maven Surefire 和 Maven Enforcer 插件,但找不到适合我需求的任何东西。如果我完全跳过测试执行,这个检查应该不会失败。
编辑:这是 Spock 示例项目的缩短版本,因为 kriegaex 建议发布一些 pom.xml 文件。不过,这个问题适用于所有 Java 项目。对我来说,在项目本身的构建过程中的解决方案会优于配置 CI/CD 作业,因为在将项目移动/迁移到另一个 CI 管道时很容易忘记这一点。
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.spockframework</groupId>
<artifactId>spock-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spock Framework - Example Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<groovy.version>3.0.7</groovy.version>
</properties>
<build>
<plugins>
<!-- Mandatory plugins for using Spock -->
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
visit https://github.com/groovy/GMavenPlus/wiki -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Optional plugins for using Spock -->
<!-- Only required if names of spec classes don't match default Surefire patterns (`*Test` etc.) -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useModulePath>false</useModulePath> <!-- https://issues.apache.org/jira/browse/SUREFIRE-1809 -->
<useFile>false</useFile>
<includes>
<include>**/*Test</include>
<include>**/*Spec</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.0-M5-groovy-3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Mandatory dependencies for using Spock -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
答案 0 :(得分:1)
抱歉对 spock-junit4
的误解。如果您想在 Spock 2 下运行 JUnit 4 扩展(例如 PowerMock runner),您只需要它。如果您想运行 JUnit 4 测试 旁边 Spock 2 测试,您需要 org.junit.vintage:junit-vintage-engine:5.7.1
史波克 2.0-M5。添加该依赖项,为我修复您的 POM。
但是,我冒昧地升级到 Spock 2.0 final、Groovy 3.0.8 和 JUnit 5.7.2,所有这些都与 Spock 2.0 保持一致。如果您使用默认的 Maven 目录布局并将 Spock 规范命名为 src/test/groovy/*Test
而不是 *Spec
,您甚至可以不用 Surefire 类名过滤器。但是,您应该指定 Java 源和目标级别。没有它们,我的 JDK 会抱怨默认的 1.5。
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.spockframework</groupId>
<artifactId>spock-example</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Spock Framework - Example Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<groovy.version>3.0.8</groovy.version>
</properties>
<build>
<plugins>
<!-- Mandatory plugins for using Spock -->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useModulePath>false</useModulePath> <!-- https://issues.apache.org/jira/browse/SUREFIRE-1809 -->
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.0-groovy-3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Required if you want to run JUnit 4 tests alongside Spock 2 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
P.S.:实际上,首先我想将您的问题作为 that one 的副本来结束。但是因为从那以后版本号发生了一些变化,我想我会在这里发布一个新的答案以方便您。
更新:您可以通过添加 *Test
或 { {1}} 个扩展。奇怪的是,*Spec
不起作用,这是一个众所周知的 Surefire 怪癖。
.java
或
.class