java覆盖率工具--to-be-continue

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhiyuan411/article/details/52603962

JaCoCo

概述

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

The focus is providing a lightweight, flexible and well documented library for integration with various build and development tools.

jacoco的目标是提供一个轻量,弹性并很好文档化的lib库,从而可以和多种多样的构建和开发工具进行集成。

特征:

Features

  • Coverage analysis of instructions (C0), branches (C1), lines, methods, types and cyclomatic complexity.
  • Based on Java byte code and therefore works also without source files.
  • Simple integration through Java agent based on-the-fly instrumentation. Other integration scenarios like custom class loaders are possible through the API.
  • Framework agnostic: Smoothly integrates with Java VM based applications like plain Java programs, OSGi frameworks, web containers or EJB servers.
  • Compatible with all released Java class file versions.
  • Support for different JVM languages.
  • Several report formats (HTML, XML, CSV).
  • Remote protocol and JMX control to request execution data dumps from the coverage agent at any point in time.
  • Ant tasks to collect and manage execution data and create structured coverage reports.
  • Maven plug-in to collect coverage information and create reports in Maven builds.

Non-Functional Characteristics

  • Simple usage and integration with existing build scripts and tools.
  • Good performance with minimal runtime overhead especially for large scale projects.
  • Lightweight implementation with minimal dependencies on external libraries and system resources.
  • Comprehensive documentation.
  • Fully documented APIs (JavaDoc) and examples for integration with other tools.
  • Regression tests with full functional test coverage based on JUnit test cases.

覆盖率支持尺度

覆盖率支持尺度:指令(C0 Coverage),分支(C1 Coverage),行,方法,类,以及Cyclomatic Complexity。

指令(C0 Coverage)

Instructions (C0 Coverage)

The smallest unit JaCoCo counts are single Java byte code instructions. Instruction coverage provides information about the amount of code that has been executed or missed. This metric is completely independent from source formatting and always available, even in absence of debug information in the class files.

该尺度统计了每一个Java字节码是否被执行过,是最小的统计尺度。
该尺度统计不需要class文件在编译时包含debug信息。

分支(C1 Coverage)

Branches (C1 Coverage)

JaCoCo also calculates branch coverage for all if and switch statements. This metric counts the total number of such branches in a method and determines the number of executed or missed branches. Branch coverage is always available, even in absence of debug information in the class files. Note that exception handling is not considered as branches in the context of this counter definition.

该尺度统计了if和switch语句的分支覆盖(判断的真假均曾被满足;若判断语句是由多个逻辑条件组合而成,则仅仅判断了其最终结果)。
该尺度统计不需要class文件在编译时包含debug信息。

行,方法,类

当代码行/方法所拥有的某个字节码被执行过时,该行/方法被认为是被执行过。
当类所拥有的某个方法被执行过时,该类被认为是被执行过。
该尺度统计 需要 class文件在编译时包含debug信息。

应用模式

Ant Tasks

JaCoCo comes with Ant tasks to launch Java programs with execution recording and for creating coverage reports from the recorded data. Execution data can be collected and managed with the tasks coverage, agent, dump and merge. Reports in different formats are creates with the report task. For offline instrumentation the task instrument can be used to prepare class files.

Ant Tasks方式支持Execution data的收集、合并,和创建覆盖率报告。

The JaCoCo Ant tasks require

  • Ant 1.7.0 or higher and
  • Java 1.5 or higher (for both, the Ant runner and the test executor).

All tasks are defined in jacocoant.jar (which is part of the distribution) and can be included in your Ant scripts with the usual taskdef declaration

Ant Tasks方式需要在Ant脚本中进行配置和修改。

Maven Plug-in

The JaCoCo Maven plug-in provides the JaCoCo runtime agent to your tests and allows basic report creation.

Maven Plug-in方式提供JaCoCo运行时代理(可以提供Execution data的收集)并可以创建基本的覆盖率报告。

The JaCoCo Maven plug-in requires

  • Maven 2.1.0 or higher and
  • Java 1.5 or higher (for both, the Maven runtime and the test executor).

Maven Plug-in方式需要在pom.xml中引入jacoco的Jar包。

Java Agent

JaCoCo uses class file instrumentation to record execution coverage data. Class files are instrumented on-the-fly using a so called Java agent. This mechanism allows in-memory pre-processing of all class files during class loading independent of the application framework.

Java Agent方式是不依赖于应用框架的,可以在载入class文件时,对class文件在内存中进行预处理。从而可以在JVM正在运行的时候,无需重启就实现Java agent,提供Execution data收集的功能.

The JaCoCo agent collects execution information and dumps it on request or when the JVM exits. There are three different modes for execution data ouput:

  • File System: At JVM termination execution data is written to a local file.
  • TCP Socket Server: External tools can connect to the JVM and retrieve execution data over the socket connection. Optional execution data reset and execution data dump on VM exit is possible.
  • TCP Socket Client: At startup the JaCoCo agent connects to a given TCP endpoint. Execution data is written to the socket connection on request. Optional execution data reset and execution data dump on VM exit is possible.

Execution data的收集方式有3种:
File System: 当JVM结束时,把Execution data写到一个本地文件。
TCP Socket Server: 可以通过TCP连接来访问Java Agent,并获取Execution data数据。
TCP Socket Client: Java Agent通过TCP连接主动把Execution data数据推送给配置的Ip:port。

猜你喜欢

转载自blog.csdn.net/zhiyuan411/article/details/52603962