您的位置:首页 > 其它

使用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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: