Maven之Cobertura Maven Plugin

发表于:2016-3-11 13:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:不详    来源:51testing软件测试网采编

  cobertura-maven-plugin是一个校验单元测试用例覆盖率的工具,可以生成一个测试覆盖率报告,可以给单元测试用例编写提供参考.
  helloword
  cobertura-maven-plugin的使用也很简单,首先你要有源码,然后要有对这个源码编写的测试代码,最后在pom.xml中配置上cobertura-maven-plugin执行一行命令就可以了.
  我们先来准备一个源码和测试用例:
  要被测试的代码
  packagecom.qyf404.learn.maven;
  publicclassApp{
  publicintadd(inta,intb){
  returna+b;
  }
  publicintsubtract(inta,intb){
  returna-b;
  }
  }
  测试用例代码
  packagecom.qyf404.learn.maven;
  importorg.junit.After;
  importorg.junit.Assert;
  importorg.junit.Before;
  importorg.junit.Test;
  importorg.junit.experimental.categories.Category;
  publicclassAppTest{
  privateAppapp;
  @Before
  publicvoidsetUp(){
  app=newApp();
  }
  @Test
  publicvoidtestAdd()throwsInterruptedException{
  inta=1;
  intb=2;
  intresult=app.add(a,b);
  Assert.assertEquals(a+b,result);
  }
  @Test()
  publicvoidtestSubtract()throwsInterruptedException{
  inta=1;
  intb=2;
  intresult=app.subtract(a,b);
  Assert.assertEquals(a-b,result);
  }
  @After
  publicvoidtearDown()throwsException{
  }
  }
  pom.xml配置如下:
  <?xmlversion="1.0"encoding="UTF-8"?>
  <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qyf404</groupId>
  <artifactId>learn-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
  <plugins>
  <plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.7</version>
  </plugin>
  </plugins>
  </build>
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
  </dependency>
  </dependencies>
  </project>
  数据都准备好了,我们执行以下maven命令mvncobertura:cobertura,执行完后会在target目录里找到site目录,用浏览器打开里面的index.html,这就是测试用例执行完后cobertura-maven-plugin得出的覆盖率报告.
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号