【问题标题】:Excluding test folder from pom doesn't work从 pom 中排除测试文件夹不起作用
【发布时间】:2019-11-07 20:35:31
【问题描述】:

我有 2 组测试。一个用于 junit 测试,另一个用于 db-unit 测试。我只想在单独的阶段执行 db-unit 测试用例,比如说“mvn integration-test”。

junit test cases folder: bso
db-unit test cases folder: dao

但是当我运行“mvn integration-test”时,它也会自动运行 junit 测试用例。所以我排除了那个名为“bso”的junit测试用例文件夹。但它仍然会在 bso 文件夹中运行测试用例。 在这里查看我的 pom 文件。如何仅在“dao”文件夹中运行测试用例?

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
            <trimStackTrace>false</trimStackTrace>
        </configuration>
        <executions>
            <execution>
              <id>unit-tests</id>
              <phase>test</phase>
              <goals>
                 <goal>test</goal>
              </goals>
              <configuration>
                  <skip>true</skip>
              </configuration>
           </execution>
           <execution>
              <id>integration-tests</id>
              <phase>integration-test</phase>
              <goals>
                 <goal>test</goal>
              </goals>
              <configuration>
                 <skip>false</skip>
                 <includes>
                   <include>**/dao/**</include>
                 </includes>
                 <excludes>
                    <exclude>**/bso/**</exclude>
                 </excludes>
              </configuration>
           </execution>
        </executions>
    </plugin>

【问题讨论】:

  • 是的,这可能是一个解决方案。但是为什么这不起作用?
  • 数据库测试应该命名为*IT.java,而其他的应该命名为*Test.java。您必须配置 maven-failsafe-plugin 并根据命名约定,单元测试将由 maven-surefire 执行,无需任何补充配置,集成测试*IT.java 将由 maven-failsafe-plugin (maven.apache.org/surefire/maven-failsafe-plugin/usage.html ) 不需要以这种方式排除/包含。

标签: maven maven-3 pom.xml


【解决方案1】:

通常,您需要使用不同的插件执行单元测试和集成测试 单元测试通常由 Maven Surefire 插件处理,集成测试由 Maven Failsafe 插件处理。

为什么? 在我的脑海中,有两个原因:

  • 如果集成测试失败,您可能不希望构建失败(并非总是如此,但仍然如此)。
  • 测试报告不同(在不同文件夹下生成)

Surefire 默认识别以*Test 结尾的测试类,Failsafe 插件与*IT 一起使用(集成测试) 所以把它们放在不同的文件夹下,不要混在一起,你就可以开始了。

完成后查看报告(在目标文件夹中,您会找到 surefire-reports 和 failsafe-reports 文件夹)并看到插件确实选择了正确的测试用例

【讨论】:

    【解决方案2】:

    从execution 中删除exclude 标记并将id 作为integration-tests 将导致在dao 包下执行测试。

    以下是相同的代码 sn-p:

    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                    <configuration>
                        <trimStackTrace>false</trimStackTrace>
                    </configuration>
                    <executions>
                        <execution>
                            <id>unit-tests</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <configuration>
                                <skip>true</skip>
                            </configuration>
                        </execution>
                        <execution>
                            <id>integration-tests</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <configuration>
                                <skip>false</skip>
                                <includes>
                                    <include>**/dao/**</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
    

    【讨论】:

    • 不,没有任何区别
    猜你喜欢
    • 2019-01-11
    • 2011-02-11
    • 2015-05-26
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    相关资源
    最近更新 更多