繁体   English   中英

使用Maven surefire插件包含测试

[英]Using the Maven surefire plugin to include tests

我正在使用Maven来构建我的项目。 我目前将测试拆分为不同的层次结构:

  • 单元测试 - > src/test/java/**/*Test.java
  • 集成测试 - > src/test-integration/java/**/*Test.java
  • 外部测试 - > src/test-external/java/**/*Test.java

这是我的maven-surefire-plugin部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
        <includes>
            <include>src/test/java/**/*Test.java</include>
        </includes>
    </configuration>
</plugin>

上面的<include>指令不起作用。 我运行时没有执行任何测试: mvn clean test

我试过**/*Test.java并运行所有测试 - 单元,集成和外部。 但是,对于默认测试套件,我只想运行单元测试。

我怎样才能在Maven中完成这项工作?

参考:

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>

您应该将您的单元和集成测试用例分开。 单元测试可以使用Surefire插件运行,并且有一个单独的插件,名为Failsafe,用于运行集成测试。

Surefire插件默认运行所有名称以*Test.java结尾的测试文件。 诀窍是用不同的名称命名集成测试文件,比如*IT.java Failsafe插件会将它们识别为集成测试。

您可以在此答案中找到示例用法 - 如何运行Maven Integration测试

如果您遵循默认设置,例如将测试用例文件放在src/test/java文件夹中,并将测试文件命名为*Test.java您也不必单独配置Surefire插件。

这是因为<include>路径是相对于<testSourceDirectory> ,它默认为${project.build.testSourceDirectory} = src / test / java。 试试这个

<include>**/*Test.java</include>

使用排除而不是包含是否更容易?

<excludes>
    <exclude>test-integration/**/*</exclude>
    <exclude>test-external/**/*</exclude>
</excludes>

或类似的东西?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM