如何确保某些测试在Maven构建中执行而不会被默默忽略?
我最近被迫切换到Groovy3。因此,我也更新了我的斯波克版本。不幸的是,我忽略了Spock2需要JUnit5这一事实。我只有JUnit4和Spock测试,因此Maven Surefire插件使用JUnit4提供程序来执行测试。所有斯波克测试均被忽略。我只是偶然注意到了这一点。
我正在寻找一种方法来检查以*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>
很抱歉对spock-junit4
有误解。如果您想在Spock2下运行JUnit4扩展,例如PowerMockrunner,那么您只需要这样做。如果要在Spock2测试之外运行JUnit4测试,则需要Spock2.0-M5的org.junit.vintage:junit-vintage-engine:5.7.1
。添加该依赖项,为我修复您的POM。
然而,我可以自由地升级到Spock2.0最终版本、Groovy3.0.8和JUnit5.7.2,所有这些都与Spock2.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>
第页。S.:事实上,首先我想把你的问题作为那个问题的重复来结束。但因为从那以后版本号发生了一些变化,为了方便您,我想在这里发布一个新的答案。
更新:您可以通过添加.java
或.class
扩展来修复问题中的筛选器,从而确保名为*Test
和*Spec
的测试都已执行。奇怪的是,.groovy
不会工作,这是众所周知的Surefire怪癖。
<includes>
<include>**/*Test.class</include>
<include>**/*Spec.class</include>
</includes>
或
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>