【问题标题】:Maven Surefire not executing test phaseMaven Surefire 未执行测试阶段
【发布时间】:2018-01-08 10:59:50
【问题描述】:

在运行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 中有以下肯定配置:

<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] ------------------------------------------------------------------------

我是否配置错误?

【问题讨论】:

  • 你能发布完整的 pom 吗?

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


【解决方案1】:

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

maven-surefire-plugin 的角度来看,Groovy 类只不过是无意义的文本文件,它无法扫描它以查找注释。但不是指定 Groovy 源,我可以成功地指定 &lt;includes&gt; 在类名方面(这个例子应该选择 一切):

            <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)

【讨论】:

    【解决方案2】:

    您是否尝试过更改工作目录:

    <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>
    

    2018 年 11 月 1 日更新: 不确定这会解决问题,但您可以尝试将这些依赖项添加为报告依赖项(在构建依赖项之外):

    <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>
    

    【讨论】:

    • 加西亚,是的,我有,它产生相同的结果。
    • 也可以试试 /**/*Spec.groovy。注意开始 /
    • 不幸的是,同样的事情 - 真正令人恼火的是,在调试中似乎没有提到 surefire:test。
    • 我已经更新了我的答案。你也可以检查一下吗?
    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2012-07-07
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多