代码覆盖度工具jacoco的入门

  jacoco官网:http://www.eclemma.org/jacoco/

手动使用方法:

1. 在tomcat的脚本catalina.sh中加入jacoco相关的配置,如下

#原有内容,220行左右
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi

   在以上原有代码后面添加以下代码:

JAVA_OPTS="$JAVA_OPTS -javaagent:/home/test_ds/jacoco/lib/jacocoagent.jar=destfile=/home/test_ds/langfangPoc/jacoco/target/jacoco.exec,append=false,includes=com.yoyosys.lfpoc.*"

 注:以上参数用来指定jacocoagent.jar路径、输出文件的路径destfile、是否允许append、包含那些类等。

2、启动tomcat、访问相关地址、停止tomcat后才会生成destfile相关文件。

3、根据官网提供的示例中,参考build.xml,或创建build.xml文件,内容如下,修改相关property: result.classes.dir、result.report.dir、result.exec.file、classpath节点。

<?xml version="1.0" encoding="UTF-8"?>

<!-- 
   Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
   All rights reserved. This program and the accompanying materials
   are made available under the terms of the Eclipse Public License v1.0
   which accompanies this distribution, and is available at
   http://www.eclipse.org/legal/epl-v10.html
  
   Contributors:
      Marc R. Hoffmann - initial API and implementation
-->

<project name="Example Ant Build with JaCoCo" default="report" xmlns:jacoco="antlib:org.jacoco.ant">
	
	<description>
	  Example Ant build file that demonstrates how a JaCoCo coverage report
	  can be itegrated into an existing build in three simple steps.
	</description>

	<property name="src.dir" location="/home/bzl/YOYO/project/ssconsole/branches/wang.jiyun/src/" />
	<property name="result.dir" location="./target" />
	<!--
	<property name="result.classes.dir" location="/home/test_ds/langfangPoc/build/classes" />
	-->
	<property name="result.classes.dir" location="/home/bzl/download/soft/apache-tomcat-6.0.35/webapps/SSConsole/WEB-INF/classes" />
	<property name="result.report.dir" location="/home/bzl/site/jacoco" />
	<property name="result.exec.file" location="/home/bzl/jacoco.exec" />

	<!-- Step 1: Import JaCoCo Ant tasks -->
	<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
		<classpath path="/home/bzl/jacoco-0.6.2.201302030002/lib/jacocoant.jar" />
	</taskdef>

	<target name="clean">
		<delete dir="${result.dir}" />
	</target>

	<target name="compile">
		<mkdir dir="${result.classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false" />
	</target>

	<target name="test" depends="">
		<!-- Step 2: Wrap test execution with the JaCoCo coverage task -->
		<jacoco:coverage destfile="${result.exec.file}">
			<java classname="org.jacoco.examples.parser.Main" fork="true">
				<classpath path="${result.classes.dir}" />
				<arg value="2 * 3 + 4"/>
				<arg value="2 + 3 * 4"/>
				<arg value="(2 + 3) * 4"/>
				<arg value="2 * 2 * 2 * 2"/>
				<arg value="1 + 2 + 3 + 4"/>
				<arg value="2 * 3 + 2 * 5"/>
			</java>
		</jacoco:coverage>
	</target>

	<target name="report" depends="">
		<!-- Step 3: Create coverage report -->
		<jacoco:report>

			<!-- This task needs the collected execution data and ... -->
			<executiondata>
				<file file="${result.exec.file}" />
			</executiondata>

			<!-- the class files and optional source files ... -->
			<structure name="JaCoCo Ant Example">
				<classfiles>
					<fileset dir="${result.classes.dir}" />
				</classfiles>
				<sourcefiles encoding="UTF-8">
					<fileset dir="${src.dir}" />
				</sourcefiles>
			</structure>

			<!-- to produce reports in different formats. -->
			<html destdir="${result.report.dir}" />
			<csv destfile="${result.report.dir}/report.csv" />
			<xml destfile="${result.report.dir}/report.xml" />
		</jacoco:report>
	</target>


</project>

 4、执行ant report 命令,即生成覆盖报告。到相关目录查看 如:file:///home/bzl/site/jacoco/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值