使用Ant与Junit进行自动化测试

本文介绍了如何利用Ant构建工具与Junit测试框架相结合,进行项目的自动化测试。内容包括项目结构说明、Calculator.java源代码展示以及测试报告的生成。参考了IBM DeveloperWorks的相关教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


使用Ant与Junit组合进行测试。

1.项目结构图:

2.文件代码

Calculator.java

package com.test.junit;

public class Calculator
{
	public int add(int a, int b)
	{
		return a+b;
	}
	public int substact(int a, int b)
	{
		return a-b;
	}
	public int mutilfy(int a, int b)
	{
		return a*b;
	}
	public int divid(int a, int b)
	{
		if(b == 0)
			throw new RuntimeException("/ by zero");
		return a/b;
	}
	public String toString()
	{
		return this.getClass().getName()+"@"+this.hashCode();
	}
	
}
CalculatorTest.java

package com.test.junit;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest
{
	private Calculator cal;
	@Before
	public void setUp() throws Exception
	{
		cal = new Calculator();
	}

	@Test
	public void testAdd()
	{
		assertEquals("相等", 5, cal.add(2, 3));
	}

	@Test
	public void testSubstact()
	{
		assertEquals("相等", -1, cal.substact(2, 3));
	}

	@Test
	public void testMutilfy()
	{
		assertEquals("相等", 6, cal.mutilfy(2, 3));
	}

	@Test
	public void testDivid()
	{
		assertEquals("相等", 2, cal.divid(6, 3));
	}
	@Test(expected=RuntimeException.class)
	public void testDividByZero()
	{
		assertEquals("异常", -1, cal.divid(3, 0));
	}

}
3.Ant脚本,build.xml

<?xml version="1.0" encoding="utf-8"?>
 <!-- ============================================= 
     ant-junit 整合                                                               
     ========================================== --> 
<project name="ant-junit" default="test" basedir="."> 

		<!--设置属性-->
		 <property name="output" value="bin"/> 
		 <property name="src" value="src"/> 
		 <property name="test" value="test"/> 
		 <property name="report" value="report" /> 
	
		 <!-- - - - - - - - - - - - - - - - - - 
          target: test report folder init                      
         - - - - - - - - - - - - - - - - - --> 
		 <target name="init"> 
			 <mkdir dir="${report}"/> 
		 </target> 
	
		 <!-- - - - - - - - - - - - - - - - - - 
          target: compile                      
         - - - - - - - - - - - - - - - - - --> 
		 <target name="compile"> 
			 <javac srcdir="${src}" destdir="${output}" includeantruntime="true" /> 
			 <echo>compilation complete!</echo> 
		 </target> 

		 <!-- - - - - - - - - - - - - - - - - - 
          target: compile test cases                      
         - - - - - - - - - - - - - - - - - --> 
		 <target name="testcompile" depends="init"> 
			 <javac srcdir="${test}" destdir="${output}" includeantruntime="true" /> 
			 <echo>test compilation complete!</echo> 
		 </target> 
	
		 <target name="allcompile" depends="compile, testcompile"></target> 
	
		 <!-- ======================================== 
          target: auto test all test case and output report file                      
      	 ===================================== --> 
		 <target name="test" depends="allcompile"> 
			 <junit printsummary="on" fork="true" showoutput="true"> 
				 <classpath> 
					 <fileset dir="lib" includes="**/*.jar"/> 
					 <pathelement path="${output}"/> 
				 </classpath> 
				 <formatter type="xml" /> 
				 <batchtest todir="${report}"> 
					 <fileset dir="${output}"> 
						 <include name="**/*Test.*" /> 
					 </fileset> 
				 </batchtest> 
			 </junit> 
			 <junitreport todir="${report}"> 
				 <fileset dir="${report}"> 
					 <include name="TEST-*.xml" /> 
				 </fileset> 
				 <report format="frames" todir="${report}" /> 
			 </junitreport> 
		 </target> 
 </project>

4.报告

参考资料:

https://www.ibm.com/developerworks/cn/java/j-lo-junit4/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值