0


集成测试,单元测试隔离 maven-surefire-plugin

详见 集成测试,单元测试隔离 maven-surefire-plugin

maven的goal生命周期

Maven生存周期 - 含 integration-test

Maven本身支持的命令(Goals)是有顺序的,越后面执行的命令,会将其前面的命令和其本身按顺序执行一遍,具体的顺序如下所示:

validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install

从上面maven的生存期可以看出

作者:哈比猪
链接:https://www.jianshu.com/p/e638d64b6955/
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

maven单元测试与集成测试

  1. 通过maven的Profile
  2. 配置生命周期 通过maven-surefire-plugin的生命周期配置不同的测试范围

如下使用的是方式2

unit包中包含的是单元测试

integration包种包含的是集成测试

<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.9</version>

                <configuration>

                    <skip>true</skip>

                </configuration>

                <executions>

                    <execution>

                        <id>run-integration-test</id>

                        <phase>integration-test</phase>

                        <goals>

                            <goal>test</goal>

                        </goals>

                        <configuration>

                            <skip>false</skip>

                            <includes>

                                <include>**/integration/**/*.java</include>

                            </includes>

                        </configuration>

                    </execution>

                    <execution>

                        <id>run-test</id>

                        <phase>test</phase>

                        <goals>

                            <goal>test</goal>

                        </goals>

                        <configuration>

                            <skip>false</skip>

                            <includes>

                                <include>**/unit/**/*.java</include>

                            </includes>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>

                <version>2.0</version>

                <configuration>

                    <dependentWarExcludes>WEB-INF/lib</dependentWarExcludes>

                </configuration>

            </plugin>

        </plugins>

maven-failsafe-plugin 配置POM.XML

实际上POM.XML提供了很强大的配置功能,这里利用一个maven的插件,叫maven-failsafe-plugin,具体配置如下:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.17</version>
        <executions>
            <execution>
                <id>integration-tests</id>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <excludes>
                        <exclude>none</exclude>
                    </excludes>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
              </configuration>
            </execution>
        </executions>
</plugin>

这里创建了一个execution,当执行integration-test goal的时候,执行包括“IT.java”结尾的所有java文件,那如何跳过unit test,integration-test呢?看下面利用了另外一个插件,叫maven-surefire-plugin,如下配置:

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.6</version>
       <configuration>
              <skip>false</skip>
       </configuration>
</plugin>

通过skip标签来指定跳过test的行为,默认是支持skipITsskipTests,前者是跳过integration-test,后者是跳过所有的测试(稍微说明下,跳过测试方法有很多种,比如maven-failsafe-plugin本身也支持加入skip选项,只不过实现之后每个命令的含义就略微有不同了,大家选择自己最习惯的就好了).

  • 推荐的测试目录结构

Unit Test Classes        : src/test/java/**/**Test.java
Integration Test Classes : src/test/java/**/**IT.java
TestCases dependent files: src/test/resources/*
标签: 集成测试

本文转载自: https://blog.csdn.net/fei33423/article/details/131964913
版权归原作者 个人渣记录仅为自己搜索用 所有, 如有侵权,请联系我们删除。

“集成测试,单元测试隔离 maven-surefire-plugin”的评论:

还没有评论