码迷,mamicode.com
首页 > 其他好文 > 详细

Jacoco+Jenkines小白之路

时间:2019-04-09 20:20:40      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:pom.xml   支持   本地   ros   团队   over   版本   超级   nal   

Jacoco+Jenkines小白之路

最近工作中正在推广jacoco的增量覆盖率的统计,想学习一波,纯粹采坑中,适合小白学习jacoco。调研了一下市面上使用的增量覆盖率的工具,发现大多数公司都在使用jacoco,那么jacoco到底相比其他工具的优点表现在哪些方面呢?

市场上主要的代码覆盖率工具:

  • Emma
  • Cobertura
  • Jacoco
  • Clover

比较:(wuli图片是我上百度copy下来的),总结一下jacoco的优势

  • JaCoCo支持分支覆盖、引入了Agent模式
  • EMMA官网已经不维护了,JaCoCo是其团队开发的,可以理解为一个升级版
  • JaCoCo社区比较活跃,官网也在不断的维护更新
  • 很多第三方的工具提供了对JaCoCo的集成,如sonar、Jenkins等
    技术图片

接下来就进入正题了,我们可以现在本地的Maven工程中插入jacoco插件学习一下,只需要3步,跟我一起学习吧

1.pom.xml文件中插入jacoco的插件及版本

<?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>jacoco</groupId>
<artifactId>jacoco</artifactId>
<version>1.0-SNAPSHOT</version>

<name>JaCoCo Examples</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Sonar -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <!-- The destination file for the code coverage report has to be set to the same value
    in the parent pom and in each module pom. Then JaCoCo will add up information in
    the same report, so that, it will give the cross-module code coverage. -->
    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>
    <sonar.language>java</sonar.language>
</properties>

<dependencies>
    <!--
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
    </dependency>
    -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.2</version>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <!--<configuration>-->
                <!--<includes>com.*</includes>-->
            <!--</configuration>-->
            <executions>
                <execution>
                    <id>pre-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
</build>


2.写一个Java方法及它的测试方法(这个测试方法就是用来测试你写的方法的并且计算你的全量覆盖率的),我写了个超级超级简单的HelloWorld方法

public class HelloWorld {

 public HelloWorld(){}
 public String Method1(){
     return "Hello World";
 }
 public int Method2(int a,int b){
     return a+b;
 }

}

public class HelloWorldTest {

@Test
public void testMethod1(){
    HelloWorld helloWorld=new HelloWorld();
    String string=helloWorld.Method1();
    Assert.assertNotNull(string);
}

}

  1. mvn clean 一下,然后在运行一下,就可以生成全量报告

技术图片
技术图片
技术图片

4.进入文件夹中找到index.html,打开就可以查看全量测报告了
技术图片

以上就是本地玩转jacoco的教程,因为Jenkins提供了对jacoco的集成,所以接下来是Jenkins+jacoco

1.首先有一台服务器并且配置好了Jenkins,需要将代码上传到git上,我的Jenkins之前就配置好了github,所以我把代码上传到github上
2.Jenkins上配置
技术图片

技术图片

3.重新构建工程,可以看到如下的效果。
技术图片

Jacoco+Jenkines小白之路

标签:pom.xml   支持   本地   ros   团队   over   版本   超级   nal   

原文地址:https://www.cnblogs.com/lixuan1998/p/10679141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!