如何使用Maven运行Spek测试?

时间:2016-07-26 14:37:57

标签: maven kotlin maven-surefire-plugin

我正在尝试使用Maven运行Spek测试。我已经设置了这样的maven:

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>

                <configuration/>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
     </build>

另外我在src / test / kotlin中有一些Spek测试。我可以在IDE中运行这些,但是当我运行mvn test时,它们被surefire插件完全忽略(它报告正在运行0个测试)。是否可以使用maven运行Spek测试,如果可以,将如何设置?我在spek主页上找不到任何相关内容。

1 个答案:

答案 0 :(得分:2)

我可以通过一个小样本项目you can find on GitLab.com重现您的问题。

如果你运行mvn clean test,surefire将找不到任何测试:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ spek-maven-sample ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.074 s
[INFO] Finished at: 2016-07-27T00:49:14+02:00
[INFO] Final Memory: 36M/363M
[INFO] ------------------------------------------------------------------------

可以通过在Surefire插件中明确包含测试规范来规避这一点。比如说,您的所有规范都命名为*Spec.*,然后在pom.xml明确配置surefire:

<build>
    <plugins>
        <!-- other build plugins like the kotlin-maven-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <includes>
                    <include>**/*Spec.*</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

将导致执行测试

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ spek-maven-sample ---
[INFO] Surefire report directory: C:\Dev\IDEA\spek-test\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running NonsenseTestableSpec
Proof that 'should return true' ran
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.077 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.335 s
[INFO] Finished at: 2016-07-27T01:02:12+02:00
[INFO] Final Memory: 26M/363M
[INFO] ------------------------------------------------------------------------

您可以找到上述存储库的完整工作示例on the branch "explicit-surefire-config"