Integration testing, unit testing isolation maven-surefire-plugin

Maven's goal life cycle

Maven life cycle - including integration-test

The commands (Goals) supported by Maven itself are ordered. The later the command is executed, the previous command and itself will be executed in order. The specific order is as follows:

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

As can be seen from the survival period of maven above



Author: Habi Pig
Link: https://www.jianshu.com/p/e638d64b6955/
Source: Jianshu
The copyright of Jianshu belongs to the author. For any form of reprint, please contact the author for authorization and indicate the source.

maven unit test and integration test

  1. Profile through maven
  2. Configure the life cycle to configure different test scopes through the life cycle of maven-surefire-plugin

The method 2 is used as follows.

The unit package contains unit tests.

The integration package contains integration tests.

<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 configuration POM.XML

In fact, POM.XML provides a very powerful configuration function. Here we use a maven plug-in called maven-failsafe-plugin . The specific configuration is as follows:

<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>

An execution is created here. When the integration-test goal is executed , all java files including the end of "IT.java" are executed, so how to skip unit test and integration-test? Look at the following and use another plugin called maven-surefire-plugin , which is configured as follows:

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

Use the skip tag to specify the behavior of skipping the test. By default, skipITs and skipTests are supported . The former skips integration-test, and the latter skips all tests (to illustrate, there are many ways to skip tests, such as maven- The failsafe-plugin itself also supports adding the skip option, but the meaning of each command is slightly different after implementation, just choose the one you are most used to).

  •  Recommended test directory structure

Unit Test Classes        : src/test/java/**/**Test.java
Integration Test Classes : src/test/java/**/**IT.java
TestCases dependent files: src/test/resources/*

Guess you like

Origin blog.csdn.net/fei33423/article/details/131964913
Recommended