Hello Maven 7 - 使用Maven进行测试
Maven在构件的特定生命周期,通过maven-surefire-plugin插件执行JUnit或TestNG的测试用例
默认的,所有在src/test/java/下符合测试命名规则的测试类都将被运行
也可以通过配置包含或排除特殊名称或位置的测试用例
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <includes> <include>**/*Tests.java</include> </includes> <excludes> <exclude>**/*DaoTests.java</exclude> </excludes> </configuration> </plugin>
测试报告
target/surefire-reports 目录下
跳过测试
命令方式:
mvn package -DskipTests
配置方式: pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin>
同时跳过编译和测试
命令方式:
mvn package -Dmaven.test.skip=true
配置方式: pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <skip>true</skip> </configuration> </plugin>
指定要运行的测试用例
mvn test -Dtest=XXXTest
mvn test -Dtest=XXX*Test
mvn test -Dtest=XXXTest,YYYTest
mvn test -Dtest=XXX*Test,YYYTest
测试覆盖率插件 cobertura-maven-plugin
打包测试代码供重用
默认的打包不包含测试代码,如需提供测试代码给他人用需配置
提供方配置插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>
使用方声明依赖
<dependency> <groupId>com.udz.xxx</groupId> <artifactId>xxxx</artifactId> <version>1.0.0 -SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency>