我有一个 Jenkins 共享库,其中有一堆类,在 groovy 中实现,位于
src/company
文件夹中。我有涵盖这些的测试,并且可以通过单独调用每个文件来在本地运行它们,例如:
groovy --classpath src test/ThingTest.groovy
但是,现在我正在寻找类似 Python 的鼻子或 pytest 的东西,我可以从 CI 运行它们,并发现/调用每个测试,然后生成一个很好的 XML 摘要。我找到了 JUnit 和 Spock,但两者似乎都需要立即提交数百行 pom/gradle/任何文件——对于 Java 生态系统之外、只想方便地运行的人来说,这感觉像是承担了很多潜在的维护负担对每个提交进行一些测试 - 特别是当我什至不需要 Jenkins 共享库的构建系统内容时,只是来测试它!
作为一名想要更好地测试 Jenkins 管道的可重用组件的新手,最好、最简单的起点是哪里?需要明确的是,我明确地尝试不重新发明轮子,但我不想仅仅为了能够编辑所需的样板文件而安装特殊的 IDE — 我希望 Java 生态系统专家能够查看这些要求并说“酷,使用 X 和 Y,以下是如何获得最低限度的设置来实现这一点”。
目前的测试看起来像这样,但数量并不多,因此如果需要,可以很容易地将它们更改为其他内容:
import groovy.test.GroovyTestCase
import org.junit.Test
import myorg.jenkins.Thing
class ThingTest extends GroovyTestCase {
@Test
public void testThing() {
// stuff followed by assertEquals
}
感谢您在更新的问题中展示示例测试。事实上,这样的测试没有任何意义。内置
GroovyTestCase
是 JUnit 3 类,但 @Test
来自 JUnit 4(JUnit 5 Jupiter 中还有另一个 @Test
具有另一个包名称)。因此,您可以完全省略 @Test
注释并依赖于类和方法命名。因此,给定一个文件 src/test/groovy/ThingTest.groovy 像这样:
import groovy.test.GroovyTestCase
class ThingTest extends GroovyTestCase {
void testPassing() {
assert true
}
void testFailing() {
assert false
}
}
您可以像这样在 Gradle 中构建并运行它:
plugins {
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.groovy:groovy:4.0.14'
testImplementation 'org.apache.groovy:groovy-test:4.0.14'
}
然后运行
gradle test
。
要使用
mvn test
在 Maven 中构建并运行相同的内容,请使用此 POM(有点啰嗦,因为 Maven 并不是那么“常规”开箱即用):
<?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>dev.aspectj</groupId>
<artifactId>Maven_POJOTestsGroovy</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groovy.version>4.0.14</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-test</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.13.1</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
此外,Java 中的这个小 POJO 测试...
public class ThingTest {
public void testPassing() {
assert true;
}
public void testFailing() {
assert false;
}
}
...需要更小的 POM:
<?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>dev.aspectj</groupId>
<artifactId>Maven_POJOTests</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
虽然 Groovy 测试间接依赖于 JUnit 3,但这个 POJO 测试甚至不需要它。
您看,您有很多选择,无需太多设置工作。对于 Spock 来说,Gradle 和 Maven 都不会多多少少。但是,如果您对 JUnit-3 风格
GroovyTest
感到满意,也许这确实是您所需要的,即使在不使用 Spock(理想情况下)或 JUnit 5(第二选择)时您会失去很多测试舒适度。