`

junit 和cobertura 在测试驱动开发中的应用

 
阅读更多

在测试驱动开发中,单元测试非常重要,而测试代码的分支覆盖率和行覆盖率同样重要,目前比较流行的单元测试工具为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 &lt;thekingant@users.sourceforge.net&gt;
	    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实例

    在测试驱动开发中,单元测试非常重要,而测试代码的分支覆盖率和行覆盖率同样重要,目前比较流行的单元测试工具为junit,而覆盖率测试工具cobertura非常好用,现在将2者集成到一块,个人感觉在现实的开发中还是比较...

    java 测试驱动开发 by Example

    Java测试驱动开发(TDD,Test-Driven Development)是一种软件开发方法论,它强调在编写实际功能代码之前先编写测试代码。TDD的核心理念是“先写测试,再写代码”,这种模式有助于确保代码的质量,减少错误,并促进...

    cobertura 1.9.4.1

    ### JUnit 和测试驱动开发 (TDD) JUnit 是一个流行的 Java 单元测试框架,它是许多开发者进行 TDD(测试驱动开发)的首选工具。TDD 强调先编写测试用例,再编写实现代码,确保每段代码都经过测试。Cobertura 与 ...

    软件测试与junit实践Java测试

    10. **TDD(测试驱动开发)**:JUnit也支持采用TDD(Test-Driven Development)模式,即先写测试,再编写能通过测试的代码,这有助于提高代码质量和可维护性。 总之,JUnit作为Java的主流测试工具,其强大功能和...

    软件测试 junit3.8.1

    3. **测试驱动开发(TDD)**:JUnit是实践TDD(Test-Driven Development)的关键工具,先编写测试,再编写使测试通过的代码,有助于提高代码质量。 4. **持续集成**:将JUnit测试集成到持续集成系统(如Jenkins、...

    junit + ant 自动化测试

    `JUnit` 和 `Ant` 是两个非常重要的工具,它们在Java开发环境中被广泛用于自动化测试和构建管理。 `JUnit` 是一个流行的Java单元测试框架,它为开发者提供了编写和运行可重复测试的简洁接口。通过`JUnit`,你可以...

    Java测试驱动开发 源码

    Java测试驱动开发(Test-Driven Development,简称TDD)是一种软件开发方法,它强调在编写实际功能代码之前,先编写测试用例。这种方式有助于确保代码的质量,减少错误,并且能够快速发现并修复问题。在本源码包中,...

    单元测试案例junit +spring mvc +springboot

    JUnit作为Java领域最流行的单元测试框架,与Spring MVC和Spring Boot的集成使得开发者能够更高效地进行测试驱动开发(TDD)或行为驱动开发(BDD)。下面将详细介绍这个主题中的相关知识点。 1. JUnit JUnit是一个...

    readyourtweets:用于试验 JUnit 和测试驱动开发的游乐场应用程序

    标题中的"readyourtweets"是一个项目名称,暗示我们正在探讨一个与Twitter相关的应用程序,而"JUnit"和"测试驱动开发(TDD)"则是关键词,表明这个项目是关于使用Java编程语言进行单元测试和TDD实践的。 JUnit是Java...

    通过spring管理的web工程的junit单元测试方法四

    在Spring框架中,进行Web工程的JUnit单元测试是软件开发中...在实际开发中,结合代码审查、TDD(测试驱动开发)和CI/CD流程,将使项目更加健壮。记得在编写测试时,保持测试的简洁性和可读性,以便于后续的维护和扩展。

    junit hamcrest最新

    - **测试驱动开发(TDD)**:JUnit是TDD的重要工具,开发者首先编写测试,然后编写代码使其通过测试,确保代码的质量。 - **持续集成(CI)**:在持续集成服务器上运行JUnit测试,可以帮助团队及时发现并修复代码问题...

    junit4.7全套

    JUnit是Java编程语言中最常用的单元测试框架之一,主要用于软件开发中的测试驱动开发(TDD)或行为驱动开发(BDD)。它的版本4.7是该工具的一个稳定版本,提供了许多功能来帮助开发者编写、运行和组织测试用例。下面将...

    junit 单元测试

    9. **测试驱动开发(TDD)** - TDD是一种开发模式,提倡先写测试,再编写满足测试的代码。JUnit是TDD实践的重要工具。 10. **持续集成(CI)** - 在持续集成环境中,如Jenkins或Travis CI,JUnit测试通常是构建过程的...

    Junit入门实验Junit入门实验

    通过这个"Junit入门实验",你不仅将学会基本的JUnit用法,还能理解测试的重要性,以及如何通过测试驱动开发(TDD)来提高代码质量。在实验一中,你可能需要实现对一个简单的数学操作类的全面测试,包括加、减、乘、除...

    浪曦][原创]Junit.3.8.详解续二.rar

    Junit是Java编程语言中广泛使用的单元测试框架,对于软件开发,尤其是进行TDD(测试驱动开发)和持续集成来说至关重要。 【描述】:描述中没有提供具体信息,但我们可以推测这可能是前一部分教程的延续,可能涵盖更...

    junit4.8.1

    JUnit 4.8.1在4.x系列中提供了许多改进和新特性,旨在提升测试的效率和灵活性。 1. **注解驱动测试**:JUnit 4引入了注解(Annotation)的概念,使得测试类和方法的声明更加简洁。例如,`@Test`注解用于标记测试...

    Manning - JUnit in Action.pdf

    此外,书中还讨论了测试驱动开发(TDD)和行为驱动开发(BDD)的实践,帮助开发者养成良好的编程习惯。 关于断言(Assertions),本书提供了详尽的指导,包括基本的assertEquals、assertTrue等,以及更复杂的比较...

    Testing Java with JUnit 5 & Mockito学习资料

    Test Driven Development (TDD)"则专门讨论TDD(测试驱动开发)实践,强调先编写测试,再编写满足这些测试的代码,以实现更好的设计和更高的代码质量。 总的来说,这份学习资料全面覆盖了使用JUnit 5进行单元测试...

    JUnit & Ant

    本文将深入探讨JUnit和Ant在Java开发中的应用及相互配合的方式。 一、JUnit简介 JUnit是一个开源的Java测试框架,基于xUnit家族,由Kent Beck和Ernst Meyer共同创建。它的主要功能是帮助开发者编写可运行的测试用例...

Global site tag (gtag.js) - Google Analytics