使用Ant与Junit进行自动化测试
2016-08-14 23:55
459 查看
使用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/
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/
相关文章推荐
- 选人控件需求PRD
- 通过OWA修改密码,提示您输入的密码不符合最低安全要求
- 排序一: 冒泡排序
- Android错误:cannot locate symbol "rand" referenced by "libgameshared.so
- 二分的两种重要模型
- OpenGL学习 : 简单的线段OpenGL程序
- poj3762 The Bonus Salary! (mcmf 区间K覆盖问题)
- popwondows简单用法
- Retrofit情景分析
- Java集群优化——dubbo+zookeeper构建高可用分布式集群
- 26最小公倍数 lowest common multiple
- python 开发简单爬虫 by CL(一)
- Leetcode 92. Reverse Linked List II (Medium) (cpp)
- one
- uva 572 Oil Deposits
- 在 Mac 端获取【网易云音乐】付费音乐的播放地址(注意下载要坚决支持正版!)
- Android 联系人数据库介绍以及对联系人的基本操作
- 灯泡的研究
- Android 联系人数据库介绍以及对联系人的基本操作
- 2016中国大学生程序设计竞赛 - 网络选拔赛