1 执行自动化单元测试的环境
apache-ant-1.7.1 配置ant_home ANT_HOME=ANT安装根目录
junit-4.5.jar 加入到工程类路径中
jdk1.6.0_02 配置相关java_home,classpath,path
JAVA_HOME=jdk安装根目录
CLASSPATH=.;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar
PATH=%JAVA_HOME%/bin
2 根据要测试的类编写好单元测试代码
类代码
package com.junit;
/**
*
* @author Huo
*/
public class Calculator {
public int add(int a,int b){
return a+b;
}
public int minus(int a,int b){
return a-b;
}
}
package com.junit;
/**
*
* @author Huo
*/
public class LinkeString {
public String getlinkeString(String str1,String str2){
String str="";
str=str1+str2;
return str;
}
}
测试代码
package test.com.junit;
import com.junit.Calculator;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Huo
*/
public class CalculatorTest {
/**
* Test of add method, of class Calculator.
*/
@Test
public void testAdd() {
System.out.println("start add...");
int a = 10;
int b = 20;
Calculator instance = new Calculator();
int expResult = 30;
int result = instance.add(a, b);
assertEquals(expResult, result);
}
/**
* Test of minus method, of class Calculator.
*/
@Test
public void testMinus() {
System.out.println("minus");
int a = 30;
int b = 20;
Calculator instance = new Calculator();
int expResult = 10;
int result = instance.minus(a, b);
assertEquals(expResult, result);
}
}
package test.com.junit;
import com.junit.LinkeString;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Huo
*/
public class LinkeStringTest {
/**
* Test of getlinkeString method, of class LinkeString.
*/
@Test
public void testGetlinkeString() {
System.out.println("getlinkeString");
String str1 = "hello";
String str2 = "world";
LinkeString instance = new LinkeString();
String expResult = "helloworld";
String result = instance.getlinkeString(str1, str2);
assertEquals(expResult, result);
}
}
package test.com.junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
*
* @author Huo
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class, LinkeStringTest.class})
public class TestAll {
}
3 开始写ant自动化脚本,放在工程的根目录下
4 开始/运行/cmd cd 到工程目录下 执行 ant
产生的单元测试报告在report目录里