使用 Maven 运行 spock 单元测试 [英] Running spock unit tests with Maven

查看:22
本文介绍了使用 Maven 运行 spock 单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在之前的一个项目中,我使用 Spock 测试框架对我的 Java 代码进行单元测试.我发现这真的很有成效,所以我试图将 Spock 测试添加到我当前使用 Maven 作为其构建工具的项目(前一个项目使用 Gradle).虽然我可以让 Maven 编译我的 Spock 测试(使用 groovy-eclipse-compiler),但我无法让 Maven 运行测试.

On a previous project I used the Spock testing framework to unit test my Java code. I found this really productive so I am trying to add Spock tests to my current project which uses Maven as its build tool (The previous project used Gradle). While I can get Maven to compile my Spock tests (using groovy-eclipse-compiler), I am unable to get Maven to run the tests.

我用两个文件做了一个简单的例子来演示我的问题:

I've made a simple example to demonstrate my problem with 2 files:

  • pom.xml
  • src/test/java/ASpec.groovy

pom.xml 的内容:

<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>my.group</groupId>
    <artifactId>my-artifact</artifactId>
    <version>0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
            <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.0.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>0.7-groovy-2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.8.0-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.1.8-01</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

ASpec.groovy 的内容:

import spock.lang.Specification

class ASpec extends Specification {

    def "Test A"(){
        // Always fail
        expect: false
    }
}

<小时>

当我执行 mvn clean test(或 mvn clean install)时,我希望我的单个单元测试运行并失败.当它被编译时,Maven 不会运行它.有谁知道如何从 Maven 运行 Spock 单元测试(或者如果可能?)


When I execute mvn clean test (or mvn clean install) I would expect my single unit test to be run and fail. While it is compiled, Maven does not run it. Does any one know how to run a Spock unit test from Maven (or if it is possible?)

(我没有将我的测试放在一个包中以保持示例简单.此外,我将我的 groovy 代码放在 src/test/java 中以避免将示例配置为从附加目录中获取源文件,再次保持示例尽可能简单.)

(I have not put my test in a package to keep the example simple. Also I have put my groovy code in src/test/java to avoid configuring the example to pick up source files from an additional directory, again to keep the example as simple as possible.)

推荐答案

Maven Surefire 按名称查找测试类.要么将类名更改为 ATest,要么重新配置 Surefire 使用的名称模式.spock-example 项目的 POM 演示了如何执行后者.

Maven Surefire finds test classes by their name. Either change the class name to ATest, or reconfigure the name pattern used by Surefire. The POM for the spock-example project demonstrates how to do the latter.

这篇关于使用 Maven 运行 spock 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆