unit-testing - 如何从默认 "maven test"和 eclipse "run as junit test"中排除特定的单元测试

标签 unit-testing maven junit maven-surefire-plugin

在我的代码中,我有两种类型的测试:快速单元测试 慢速性能测试 .我试图确保性能测试永远不会运行,除非用户明确想要运行它们(例如,使用正确的测试套件或 maven 构建配置文件),因为这会启动网格上计算密集型的远程进程。

我了解如何使用 maven-surefire-plugin 创建排除(或包含)特定测试的构建配置文件,如本 site 所述。 .然而,我的问题是我没有找到 一种从默认运行中排除测试的方法,同时仍提供在需要时运行测试的选项 .默认运行我的意思是:

  • 在eclipse中右键项目,选择“run as|Maven test”
  • 右键单击 src/test/java 并选择 run as junit test

  • 在上述两种情况下,它运行所有单元测试(包括我希望默认排除的慢测试)。如果我尝试运行的构建配置文件中有错字,也会发生同样的事情。

    有没有一种方法可以将性能测试排除在默认运行之外,而不必将它们移动到单独的 maven 模块?

    最佳答案

    我们在项目中所做的就是用一个特殊的标记接口(interface)来标记慢速单元测试。@Cathegory(SlowTest.class)注释并将它们添加到故障安全插件中。

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-failsafe-plugin</artifactId>
       <configuration>
           <groups>com.whatever.SlowTest</groups>
       </configuration>
        <executions>
          <execution>
            <configuration>
                <includes>
                    <include>**/*.class</include>
                </includes>
            </configuration>
            <goals>
                <goal>integration-test</goal>
                </goals>
        </execution>
    </executions>
    

    如果您不想执行它们:使用 maven 选项 -DskipITs。这将跳过它们。

    关于unit-testing - 如何从默认 "maven test"和 eclipse "run as junit test"中排除特定的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505052/

    相关文章:

    java - Mockito 单元测试用例调用不明确(需要使其不明确)

    c# - 使用c#读取Excel文件中的行

    maven - 无法使用Maven执行wsdl2java

    maven - Maven插件参数的用户属性是什么意思

    java - 如何仅在构建后运行 junit 测试?

    node.js - 如何编写 Mocha 测试,其中一个测试取决于下一个测试的结果?

    .net - 如何起订功能

    java - mvn appengine :update and mvn appengine:deploy in Google App Engine 之间的区别

    java - JUnit 类型安全的 assertEquals

    java - SpringBoot 和 JUnit - 测试服务类 - 无法加载应用程序上下文