在测试驱动开发中,单元测试非常重要,而测试代码的分支覆盖率和行覆盖率同样重要,目前比较流行的单元测试工具为junit,而覆盖率测试工具cobertura非常好用,现在将2者集成到一块,个人感觉在现实的开发中还是比较实际的,下面看一下集成的过程。
1、工程目录如下:
2、编写类
public class OperateType {
public static final String ADD = "add";
public static final String SUB = "subtract";
public static final String MULT = "multiply";
public static final String DIV = "divide";
public String getType(int typeNumber) {
if (typeNumber == 1) {
return ADD;
} else if (typeNumber == 2) {
return SUB;
} else if (typeNumber == 3) {
return MULT;
} else if (typeNumber == 4) {
return DIV;
} else {
return null;
}
}
}
编写测试类
public class OperateTypeTest {
private OperateType operateType ;
@Before
public void setUp() throws Exception {
operateType = new OperateType();
}
@After
public void tearDown() throws Exception {
operateType = null;
}
@Test
public void testGetType() {
//要达到测试的覆盖率,需要进行充分的验证,所以需要验证5个
Assert.assertEquals(OperateType.ADD,operateType.getType(1));
Assert.assertEquals(OperateType.SUB,operateType.getType(2));
Assert.assertEquals(OperateType.MULT,operateType.getType(3));
Assert.assertEquals(OperateType.DIV,operateType.getType(4));
Assert.assertEquals(null,operateType.getType(5));
}
}
3、配置cobertura信息
build.properties
# The source code for the examples can be found in this directory
src.dir=../src/main/java
test.dir=../src/test/java
# The path to cobertura.jar
cobertura.dir=../cobertura
# Classes generated by the javac compiler are deposited in this directory
classes.dir=classes
# Instrumented classes are deposited into this directory
instrumented.dir=instrumented
# All reports go into this directory
reports.dir=reports
# Unit test reports from JUnit are deposited into this directory
reports.xml.dir=${reports.dir}/junit-xml
reports.html.dir=${reports.dir}/junit-html
# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.html.dir=${reports.dir}/cobertura-html
bulid.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="cobertura.examples.basic" default="coverage" basedir=".">
<description>
Cobertura - http://cobertura.sourceforge.net/
Copyright (C) 2003 jcoverage ltd.
Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net>
Cobertura is licensed under the GNU General Public License
Cobertura comes with ABSOLUTELY NO WARRANTY
</description>
<property file="build.properties" />
<property name="project.lib.dir" location="../lib" />
<!--
<property name="cobertura.dir" location="../cobertura" />
-->
<!-- cobertura lib path -->
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="lib/*.jar" />
</fileset>
</path>
<!-- project lib path -->
<path id="project.classpath">
<fileset dir="${project.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
<!--Clean task-->
<target name="clean" description="Remove all files created by the build/test process.">
<delete dir="${classes.dir}" />
<delete dir="${instrumented.dir}" />
<delete dir="${reports.dir}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
</target>
<!--int task-->
<target name="init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${reports.xml.dir}" />
<mkdir dir="${reports.html.dir}" />
<mkdir dir="${coverage.xml.dir}" />
<mkdir dir="${coverage.html.dir}" />
</target>
<!--compile task-->
<target name="compile" depends="init">
<javac encoding="UTF-8" srcdir="${src.dir}" destdir="${classes.dir}" debug="true">
<classpath>
<path refid="project.classpath"/>
<path refid="cobertura.classpath"/>
</classpath>
</javac>
<javac srcdir="${test.dir}" destdir="${classes.dir}" debug="true">
<classpath>
<path refid="project.classpath"/>
<path refid="cobertura.classpath"/>
</classpath>
</javac>
</target>
<target name="instrument" depends="init,compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<delete dir="${instrumented.dir}" />
<!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
*******************************************
-->
<include name="com/example/OperateType.class" />
</fileset>
</cobertura-instrument>
</target>
<target name="test" depends="init,compile">
<junit fork="yes" dir="${basedir}" failureProperty="test.failed">
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes. This is important.
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />
<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your classpath.
-->
<classpath refid="project.classpath" />
<classpath refid="cobertura.classpath" />
<formatter type="xml" />
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${test.dir}">
<!--
Instrument all the test classes, but
don't instrument the application classes.
*******************************************
-->
<include name="com/example/OperateTypeTest.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${reports.xml.dir}">
<fileset dir="${reports.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.html.dir}" />
</junitreport>
</target>
<target name="coverage-check">
<cobertura-check branchrate="34" totallinerate="100" />
</target>
<target name="coverage-report">
<!--
Generate an XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
</target>
<target name="alternate-coverage-report">
<!--
Generate a series of HTML files containing the coverage
data in a user-readable form using nested source filesets.
-->
<cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
</cobertura-report>
</target>
<target name="coverage" depends="clean,init,compile,instrument,test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
</project>
运行ant,脚本,产生报告,包括junit的报告和覆盖率的报告
- 大小: 44 KB
- 大小: 49.3 KB
分享到:
相关推荐
在测试驱动开发中,单元测试非常重要,而测试代码的分支覆盖率和行覆盖率同样重要,目前比较流行的单元测试工具为junit,而覆盖率测试工具cobertura非常好用,现在将2者集成到一块,个人感觉在现实的开发中还是比较...
Junit+ant+cobertura示例源码程序。
详细文档。 博文链接:https://dendrobium.iteye.com/blog/122752
内容实际Java开发中单元测试常遇到的问题JUnit4的新特性和扩展机制JUnit4扩展的实现通过测试用例实例展示JUnit扩展的执行效果结论参考资料简介: 在采用测试驱动开发的项目中,有一个经常困扰开发者的问题是:当...
测试驱动开发With Junit 第一章 前言 2 第二章 JUnit快速入门 2 第三章 JUnit测试套件 7
JUnit 是Java语言事实上的标准单元测试库。JUnit的易用性无疑是它受欢迎的主要原因。也许你会这样想:它做的事情不多,仅仅是做一些测试然后报告结果,JUnit应该是很简单的。
cobertura junit java 单元测试 测试驱动
junit junit junit junit junit junit junit junit junit 测试驱动开发
TDD的概念与模式第二部分 针对特定技术应用TDD第5章 测试驱动Web组件第6章 测试驱动数据访问第7章 测试驱动不可预测功能第8章 测试驱动Swing代码第三部分 基于ATDD构建产品第9章 解析验收测试驱动开发第10章 ...
在Eclipse中使用JUnit4进行单元测试!
Junit单元测试是一个功能强大且灵活的测试框架,广泛应用于软件开发中,可以帮助开发者提高测试效率和代码质量。但是,Junit单元测试也存在一些缺点,需要开发者具备较高的编程技能和配置技能。
在Eclipse中使用JUnit4进行单元测试 在Eclipse中使用JUnit4进行单元测试
java web 开发中,利用Struts+Spring+Hibernate框架开发网页后的测试
junit测试测试junit测试测试junit测试测试junit测试测试
junit3 junit4 api,单元测试的利器
多数Java的开发环境都已经集成了JUnit作为单元测试的工具。 [1] JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为...
Junit单元测试Junit单元测试Junit单元测试Junit单元测试
对Ada语言来说,开发人员可以选择是在独立的过程和函数,还是在Ada包的级别上进软件测试中Junit单元测试实例在一种传统的结构化编程语言中,比如C,要进行测试的单元一般是函数或子过程。在象C++这样的面向对象的...