利用JaCoCo统计接口测试中代码覆盖率
北漂的雷子 人气:9 做接口测试,很多时候都会听到,你接口测试的覆盖率是多少?很多人会回答80%,你怎么统计的,他说覆盖了80%的需求。这个回答没有错误,但是片面,我们不能只考虑需求的覆盖率,还有业务的覆盖率,场景的覆盖率,接口的覆盖率,代码的覆盖率等,本文介绍接口测试的代码覆盖率。那么我们来看看如何是实现的。
1、环境的搭建
1.1搭建 ant 环境
https://ant.apache.org/bindownload.cgi
我下载的是1.10.7版本,这个是因为 每个版本对应的java的版本 不一样,这个在ant的官网有介绍,下载的zip包 ,然后解压,然后去配置环境变量,我用的是mac配置的,打开:vi .bash_profile
export ANT_HOME=/Users/lileilei/Downloads/apache-ant-1.10.7
export PATH=$PATH:.:${ANT_HOME}/bin
配置完毕后source .bash_profile 立即生效
到这里,我们已经设置好了我们的ant的环境。
1.2 下载JaCoCo。
下载地址:https://www.jacoco.org/jacoco/
下载完毕后,解压即可。
以上搭建了所需的环境。
2.ant的build文件配置
通过build.xml拉去覆盖率,具体配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.plan</groupId> <artifactId>demo</artifactId> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot<https://img.qb5200.com/download-x/description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> <https://img.qb5200.com/download-x/dependency> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <https://img.qb5200.com/download-x/dependency> <!-- 解决FluentIterable.class找不到问题 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>26.0-jre</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.10.0</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <scope>provided</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.evosuite</groupId> <artifactId>evosuite-standalone-runtime</artifactId> <version>1.0.6</version> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.2.Final</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.2.Final</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </exclusion> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </exclusion> </exclusions> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.offbytwo.jenkins</groupId> <artifactId>jenkins-client</artifactId> <version>0.3.8</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>RELEASE</version> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>2.1.6.RELEASE</version> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.0.0</version> <scope>compile</scope> <https://img.qb5200.com/download-x/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <https://img.qb5200.com/download-x/dependency> <https://img.qb5200.com/download-x/dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warName>plan</warName> </configuration> </plugin> <plugin> <groupId>org.evosuite.plugins</groupId> <artifactId>evosuite-maven-plugin</artifactId> <version>1.0.6</version> </plugin> </plugins> </build> </project>
这是完整的配置文件,我在配置的地方加好注释,大家可以根据自己的需求进行设置。
然后去启动要测试的待测服务,这里我是用的jar 启动的服务,启动方式。
java -javaagent:/Users/lileilei/Downloads/jacoco-0.8.5/lib/jacocoagent.jar=dumponexit=true,destfile=jacoco.exe -jar demo-2.0.6.RELEASE.jar
这里需要javaagent,这里是jacoco的agent,需要替换成自己的路径就可以。
启动完成后,就可以开始进行测试了,
我使用的swgger2,进行了简单的接口测试,后获取覆盖率。
在build.xml目录,使用ant获取jacoco.exe
然后用 ant report产生测试报告
打开测试报告,
这样,我们就获取到了接口测试的全量代码的覆盖率。我们可以打开获取每个类,每个函数的覆盖率。
其实在真正的覆盖率,我们还需要考虑 增量代码覆盖率。这里简单的对全量代码的覆盖率做介绍。抛砖引玉,大家可以继续探索。
这里给大家提供了一种接口测试的覆盖率的统计方式。当然了还有其他方式,大家可以根据自己的需求进行去探索。
需要明确,提供合理方案。
方案合理,解决合理需求。
控制合理,合理控制时间。
思路清晰,善于解决问题。
思路方案,取决于实践中。
大量锻炼,才能思维敏捷。
闭门造车,僵化思路难成。
点滴积累,成就美好明天。
关注雷子说测试
加载全部内容