maven集成单元测试插件 单元测试maven
程序员文章站
2022-03-13 11:25:17
...
1.maven不可允许忽略单元测试
2.引用jacoco.version
<jacoco.version>0.7.7.201606060606</jacoco.version>
3.maven依赖jar包
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.4</version>
<scope>test</scope>
</dependency>
<!-- test jndi datasource -->
4.maven插件
<plugins>
<!-- 单元测试统计需要的插件开始 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- <useSystemClassLoader>false</useSystemClassLoader> ??? -->
<suiteXmlFiles>
<suiteXmlFile>src/test/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-XX:NewSize=256m -XX:MaxNewSize=512m -XX:PermSize=256m
-XX:MaxPermSize=512m</argLine>
<testFailureIgnore>true</testFailureIgnore>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<!-- cobertura插件 ,取消不需要计算覆盖率的类 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<!-- <instrumentation> <includes> <include>**/*Impl.class</include>
</includes> </instrumentation> -->
<check>
<branchRate>75</branchRate>
<lineRate>75</lineRate>
<haltOnFailure>false</haltOnFailure>
<totalBranchRate>75</totalBranchRate>
<totalLineRate>75</totalLineRate>
<packageLineRate>75</packageLineRate>
<packageBranchRate>75</packageBranchRate>
<!-- <regexes> <regex> <pattern>com.example.reallyimportant.*</pattern>
<branchRate>90</branchRate> <lineRate>80</lineRate> </regex> <regex> <pattern>com.example.boringcode.*</pattern>
<branchRate>40</branchRate> <lineRate>30</lineRate> </regex> </regexes> -->
</check>
</configuration>
<!-- <executions> <execution> <goals> <goal>clean</goal> <goal>check</goal>
</goals> </execution> </executions> -->
</plugin>
<!-- 单元测试统计需要的插件结束 -->
</plugins>
5.src/test目录下创建testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="app" >
<packages>
<package name="com.suning.fsp.weixin.controller.portal.app.weChat.test" />
</packages>
</test>
</suite>
2.引用jacoco.version
<jacoco.version>0.7.7.201606060606</jacoco.version>
3.maven依赖jar包
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.4</version>
<scope>test</scope>
</dependency>
<!-- test jndi datasource -->
4.maven插件
<plugins>
<!-- 单元测试统计需要的插件开始 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- <useSystemClassLoader>false</useSystemClassLoader> ??? -->
<suiteXmlFiles>
<suiteXmlFile>src/test/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-XX:NewSize=256m -XX:MaxNewSize=512m -XX:PermSize=256m
-XX:MaxPermSize=512m</argLine>
<testFailureIgnore>true</testFailureIgnore>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<!-- cobertura插件 ,取消不需要计算覆盖率的类 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<!-- <instrumentation> <includes> <include>**/*Impl.class</include>
</includes> </instrumentation> -->
<check>
<branchRate>75</branchRate>
<lineRate>75</lineRate>
<haltOnFailure>false</haltOnFailure>
<totalBranchRate>75</totalBranchRate>
<totalLineRate>75</totalLineRate>
<packageLineRate>75</packageLineRate>
<packageBranchRate>75</packageBranchRate>
<!-- <regexes> <regex> <pattern>com.example.reallyimportant.*</pattern>
<branchRate>90</branchRate> <lineRate>80</lineRate> </regex> <regex> <pattern>com.example.boringcode.*</pattern>
<branchRate>40</branchRate> <lineRate>30</lineRate> </regex> </regexes> -->
</check>
</configuration>
<!-- <executions> <execution> <goals> <goal>clean</goal> <goal>check</goal>
</goals> </execution> </executions> -->
</plugin>
<!-- 单元测试统计需要的插件结束 -->
</plugins>
5.src/test目录下创建testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="app" >
<packages>
<package name="com.suning.fsp.weixin.controller.portal.app.weChat.test" />
</packages>
</test>
</suite>
推荐阅读
-
Docker使用 Maven 插件构建镜像的方法
-
多模块maven的deploy集成gitlab ci自动发版配置
-
maven项目集成Quartz定时任务框架,实现批处理功能
-
多模块maven的deploy集成gitlab ci自动发版配置
-
Spring MVC 学习总结(十一)——IDEA+Maven+多模块实现SSM框架集成
-
springboot~maven集成开发里的docker构建
-
maven tomcat7插件启动报错
-
【IDEA使用技巧】(5) —— IntelliJ IDEA集成Tomcat部署Maven Web项目
-
在CentOS7上搭建Jenkins+Maven+Git持续集成环境的方法
-
Jetty与Maven集成