Java 使用 Maven surefire 插件来包含测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20673308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:30:21  来源:igfitidea点击:

Using the Maven surefire plugin to include tests

javaunit-testingmavenmaven-surefire-plugin

提问by kevinarpe

I am using Maven to build my project. I currently split testing into different hierarchies:

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

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

Here is my maven-surefire-pluginsection:

这是我的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>

The <include>directive above does not work. No tests are executed when I run: mvn clean test

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

I tried **/*Test.javaand it runs all the tests -- unit, integration, and external. However, for the default test suite, I only want to run the unit tests.

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

How can I make this work in Maven?

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

Ref:

参考:

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

回答by EdH

Would it be easier to use an exclusion rather than the inclusion?

使用排除比包含更容易吗?

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

Or something like that?

或类似的东西?

回答by msenni

You should separate your unit and integration test cases. Unit tests can be run using Surefire plugin and there is a separate plugin called Failsafefor running integration tests.

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

Surefire plugin by default runs all test files whose name ends with *Test.java. The trick is to name your integration test files with a different name, say *IT.java. Failsafe plugin will identify them as integration tests.

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

You can find a sample usage in this answer - How do I get my Maven Integration tests to run

您可以在此答案中找到示例用法 - How do I get my Maven Integration tests to run

Also you dont have to separately configure Surefire plugin if you follow the default setup like putting your test case files in src/test/javafolder and name your test files as *Test.java.

此外,如果您遵循默认设置,例如将测试用例文件放在src/test/java文件夹中并将测试文件命名为*Test.java.

回答by Evgeniy Dorofeev

This is because <include>path is relative to <testSourceDirectory>which defaults to ${project.build.testSourceDirectory}= src/test/java. Try this

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

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