Junit+ant+JaCoCo集成使用
接上文,本文主要介绍Junit+ant+JaCoCo集成使用
1、工具的下载上传
需要的工具有:
apache-ant-1.10.5-bin.tar
apache-tomcat-8.0.50.tar
jacoco-0.8.4-20190222.010339-7.zip
下载jacoco上传到到具体服务器:
也可以到官网下载:https://www.eclemma.org/jacoco/

下载tomcat上传到服务器:https://tomcat.apache.org/

下载ant上传到服务器:http://ant.apache.org/bindownload.cgi

上传到具体的服务器,目录如下:

2、解压配置环境变量
tar -xvf apache-tomcat-8.0..tar.gz
tar -xvf apache-tomcat-8.0..tar
unzip jacoco-0.8.-20190222.010339-.zip
2.1、设置用户环境变量:输入命令:vim ~/.bashrc,有的环境是vim ~/.bash_profile,打开文件,输入ant需要的环境变量;如下内容:
ANT_HOME=/home/antjacocolinux/apache-ant-1.10.
PATH=$ANT_HOME/bin:$PATH
export PATH ANT_HOME

2.2 退出并保存:esc => shift + : => wq => 回车,设置环境变量后必须使命令生效!!再输入命令使之生效:source ~/.bash_profile

修改apache-tomcat-8.0.50目录下bin文件夹下的catalina.sh的JAVA_OPTS配置,如图新增:

JAVA_OPTS="$JAVA_OPTS -javaagent:/home/antjacocolinux/jacoco-0.8.4/lib/jacocoagent.jar=includes=*,output=tcpserver,append=true,port=6101,address=*"
3、准备好ant项目,修改build.xml配置:
创建一个java工程,编写Calculator类
public class Calculator {
public int evaluate(String expression) {
int sum = ;
for (String summand: expression.split("\\+"))
sum += Integer.valueOf(summand);
return sum;
}
public int missAdd(int a, int b) {
return a + b;
}
public boolean beyondZero(int v) {
if (v > ) {
return true;
} else {
return false;
}
}
}
创建单元测试CalculatorTest 类
import static org.junit.Assert.assertEquals;
import org.junit.Test; public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(, sum);
} @Test
public void testChoose() {
Calculator calculator = new Calculator();
assertEquals(true, calculator.beyondZero());
}
}
修改build.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project name="junit-example" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">
<description>
这是一个简单的Demo,用来演示junit+ant+Jacoco
</description>
<property name="main.build.dir" value="build/main"/>
<property name="main.src.dir" value="src/main/java"/>
<property name="test.build.dir" value="build/test"/>
<property name="test.src.dir" value="src/test/java"/>
<property name="result.dir" location="./target" />
<property name="result.report.dir" location="${result.dir}/site/jacoco" />
<property name="result.exec.file" location="${result.dir}/jacoco.exec" /> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="lib/jacocoant.jar" />
</taskdef> <target name="clean">
<delete dir="${result.dir}" />
<delete dir="build" />
</target> <path id="classpath.test">
<pathelement location="lib/junit-4.12.jar"/>
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="${main.build.dir}"/>
</path> <target name="compile">
<mkdir dir="${main.build.dir}"/>
<javac srcdir="${main.src.dir}" destdir="${main.build.dir}" debug="true" includeantruntime="false"/>
</target> <target name="test-compile" depends="compile">
<mkdir dir="${test.build.dir}"/>
<javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
<classpath refid="classpath.test"/>
</javac>
</target> <target name="test" depends="test-compile">
<jacoco:coverage destfile="${result.exec.file}">
<junit printsummary="yes" haltonfailure="yes" fork="true" forkmode="once">
<classpath>
<path refid="classpath.test"/>
<pathelement location="${test.build.dir}"/>
</classpath>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${test.src.dir}" includes="**/*Test.java" />
</batchtest>
</junit>
</jacoco:coverage>
</target>
<target name="report" depends="test">
<jacoco:report>
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${main.build.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${main.src.dir}" />
</sourcefiles>
</structure>
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
<target name="rebuild" depends="clean,report" />
</project>
将该项目放到apache-tomcat-8.0.50/webapps下,到输入命令:
ant report
在对应的目录下会生成html的报告,目录如下:

点击可以查看具体的报告:

相关代码下载:代码下载
Junit+ant+JaCoCo集成使用的更多相关文章
- 将jacoco集成到测试工具平台
最近在做接口测试,想通过代码覆盖率来判断一下接口用例是否缺失,但是每次通过命令来生成覆盖率报告,感觉太麻烦,所以就想着把jacoco集成到测试工具平台中,只需要点几个按钮,就能查看到覆盖率报告. 测试 ...
- 单元测试系列之一:如何使用JUnit、JaCoCo和EclEmma提高单元测试覆盖率
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6726664.html -----如 ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(二)
关键字:JUnit,Ant,单元测试 终于把JUnit+Ant构建单元测试的大概了解了,其实我实践的过程是对了,只是指导博客(看到这里不懂请看我上一篇博客)本身的错误“成功”把我带入“坑”,有时候网友 ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(一)
指导博客:https://blog.csdn.net/Cceking/article/details/51692010 基于软件测试的需求,使用JUnit+Ant构建自动的单元测试. IDE:ecli ...
- jenkins+jacoco+ant+apache集成统计web端功能测试覆盖率
一.覆盖率定义 作为一个测试人员,保证产品的软件质量是其工作首要目标,为了这个目标,测试人员常常会通过很多手段或工具来加以保证,覆盖率就是其中一环比较重要的环节. 我们通常会将测试覆盖率分为两个部分, ...
- DevOps之Pipeline集成junit、jacoco、SonarQube(二)
一.准备工作 1.准备一个持续集成的代码工程 工程下载地址: Github地址为:https://github.com/zbbkeepgoing/springboot-demo 2.springboo ...
- SpringBoot Junit Maven JaCoCo
写一下最近写单体测试的一些笔记. SrpingBoot的测试用例: @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ...
- ant+Jacoco 统计tomcat远程部署后项目接口自动化测试或者功能测试代码覆盖率
1.安装ant 环境,https://ant.apache.org/bindownload.cgi 2.下载jacoco包 https://www.eclemma.org/jacoco/ ,解压后, ...
- Jenkins + Jmeter +Ant自动化集成环境搭建(一)
所需工具 一.jmeter 工具下载 https://jmeter.apache.org/ 配置环境JDK等及各种插件可以看小七之前的教程 二.Ant安装(http://ant.apache.org ...
随机推荐
- ffmpeg nvenc编码
花时间研究了一些ffmpeg的nvenc,本来想我已经有了cuvid,然后又搞出来了nvenc,应该可以做个全套的英伟达的转码了,没想到ffmpeg官网下载的动态库没有cuvid,windows上编译 ...
- CentOS7 LDAP 2.4 安装配置
软件安装 # yum -y install openldap-servers openldap-clients # systemctl start slapd # systemctl enable s ...
- 伪基站,卒于5G——本质上是基于网络和UE辅助的伪基站检测,就是将相邻基站的CI、信号强度等信息通过测量报告上报给网络,网络结合网络拓扑、配置信息等相关数据,对所有数据进行综合分析,确认在某个区域中是否存在伪基站
伪基站,卒于5G from:https://www.huxiu.com/article/251252.html?h_s=h8 2018-07-05 21:58收藏27评论6社交通讯 本文来自微 ...
- 026——VUE中事件修饰符之使用$event与$prevent修饰符操作表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 由浅入深了解EventBus:(三)
原理 EventBus的核心工作机制如下图 在EventBus3.0架构图: EventBus类 在EventBus3.0框架的内部,核心类就是EventBus,订阅者的注册/订阅,解除注册,以及事件 ...
- Java复习6异常处理
Java复习6.异常处理 20131005 前言: Java中的异常处理机制是非常强大的,相比C++ 来说,更加系统.但是我们开发人员没有很好的使用这一点.一些小的程序是没有什么问题的,但是对于大型项 ...
- hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...
- 什么是Quartz?
什么是Quartz Quartz是一个开源的作业调度框架,Quartz根据用户设定的时间规则来执行作业,使用场景:在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知. ...
- hdu 6034 Balala Power!
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- js 倒计时 时间戳
功能:传入一个截止时间(unix时间戳),显示倒计时 因为unix时间戳,并不等于js 的new Date().getTime()得到的那一串毫秒数,所以要在JS中使用unix时间戳,必须先转换一下u ...