Maven Surefire没有执行测试阶段

时间:2018-01-08 10:59:50

标签: java maven unit-testing groovy maven-surefire-plugin

当运行mvn test时,似乎surefire没有执行它的测试目标(或者至少没有接受我在配置中包含的测试。

这是一个多模块maven项目,目前全部都是groovy,结构类似于下面的结构:

root
-commons
-framework
-generatedsources1
-generatedsources2
-test-groups
--test-group1
---src/test/java/path.to.TestClass.groovy
--test-group2

我在test-groups pom.xml中有以下surefire配置:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec.groovy</include>
                    <include>**/*Test.groovy</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

但是当我对这个pom.xml或其中一个子pom执行mvn test时,测试阶段永远不会执行。

这是maven输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building system-test-category-man 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- gmavenplus-plugin:1.6:compile (default) @ system-test-category-man ---
[INFO] Using Groovy 2.4.9 to perform compile.
[INFO] Compiled 2 files.
[INFO] 
[INFO] --- gmavenplus-plugin:1.6:compileTests (default) @ system-test-category-man ---
[INFO] Using Groovy 2.4.9 to perform compileTests.
[INFO] Compiled 3 files.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.809 s
[INFO] Finished at: 2018-01-08T10:53:29Z
[INFO] Final Memory: 20M/175M
[INFO] ------------------------------------------------------------------------

当我直接调用插件时:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building system-test-category-man 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-cli) @ system-test-category-man ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.315 s
[INFO] Finished at: 2018-01-08T10:58:18Z
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------

我错误配置了什么吗?

2 个答案:

答案 0 :(得分:1)

这个问题也让我感到困惑,但我想我已经发现问题所在了。

maven-surefire-plugin 的角度来看,Groovy 类只不过是无意义的文本文件,它无法扫描注释。但是,我可以根据类名成功指定 <includes>,而不是指定 Groovy 源(此示例应该选择所有内容):

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </configuration>
            </plugin>

否则,您必须根据此处记录的默认命名约定命名您的测试(您的 Groovy 测试类):http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes 并如下所示:

<includes>
    <include>**/Test*.java</include>
    <include>**/*Test.java</include>
    <include>**/*Tests.java</include>
    <include>**/*TestCase.java</include>
</includes>

(在这里将 .java 替换为 .groovy)

答案 1 :(得分:0)

您是否尝试过更改workingDirectory:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <workingDirectory>wherever you have your tests</workingDirectory>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec.groovy</include>
                    <include>**/*Test.groovy</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

更新时间:11/01/2018: 不确定这会解决问题,但您是否可以尝试将这些依赖项添加为报告依赖项(在构建依赖项之外):

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <showSuccess>true</showSuccess>
                <linkXRef>false</linkXRef>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
        </plugin>
    </plugins>
</reporting>