ant+junit实现TDD,自动测试
来源:互联网 发布:win7系统激活软件 编辑:程序博客网 时间:2024/05/16 08:53
呵呵,初次接触自动测试,记录一下...
首先,使用junit编写测试类,就不详细写了..帖出代码:
package src.test;
import junit.framework.TestCase;
import org.junit.*;
import src.baseline.Hello;
import static junit.framework.Assert.*;
import static junit.runner.BaseTestRunner.*;
public class TestHello extends TestCase...{
private int x = 1;
private int y = 1;
@Test
public void testSayHello()...{
Hello h = new Hello();
assertEquals(" hello, Junit 4",h.sayHello());
}
@Before
public void startTest()...{
System.out.println(" test start");
}
@After
public void endTest2()...{
System.out.println("test end");
}
}
import junit.framework.TestCase;
import org.junit.*;
import src.baseline.Hello;
import static junit.framework.Assert.*;
import static junit.runner.BaseTestRunner.*;
public class TestHello extends TestCase...{
private int x = 1;
private int y = 1;
@Test
public void testSayHello()...{
Hello h = new Hello();
assertEquals(" hello, Junit 4",h.sayHello());
}
@Before
public void startTest()...{
System.out.println(" test start");
}
@After
public void endTest2()...{
System.out.println("test end");
}
}
<project name="autotest.project" default="runtest" basedir=".">
<property name="src.dir" value="src/baseline"/>
<property name="test.dir" value="src/test"/>
<property name="build.dir" value="build/app"/>
<property name="build.test.dir" value="build/test"/>
<target name="JUNIT">
<available property="junit.present" classname="junit.framework.TestCase" />
</target>
<target name="compile" depends="JUNIT">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
classpath="lib/*.jar"
debug="on"
/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${build.dir}/lib"/>
<jar destfile="${build.dir}/lib/src.jar"
basedir="${build.dir}"></jar>
</target>
<target name="compiletests" depends="jar">
<mkdir dir="${build.test.dir}"/>
<javac srcdir="${test.dir}"
destdir="${build.test.dir}"
debug="on">
<classpath>
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
</classpath>
</javac>
</target>
<target name="runtest" depends="compiletests" if="junit.present">
<java fork="yes" classname="junit.textui.TestRunner" taskname="junit">
<arg value="src.test.TestHello"/>
<classpath>
<pathelement location="lib/junit-4.4.jar"/>
<pathelement location="${test.dir}"/>
<pathelement location="${build.dir}"/>
<pathelement location="${build.test.dir}"/>
<pathelement location="${java.class.path}"/>
</classpath>
</java>
</target>
</project>
<property name="src.dir" value="src/baseline"/>
<property name="test.dir" value="src/test"/>
<property name="build.dir" value="build/app"/>
<property name="build.test.dir" value="build/test"/>
<target name="JUNIT">
<available property="junit.present" classname="junit.framework.TestCase" />
</target>
<target name="compile" depends="JUNIT">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
classpath="lib/*.jar"
debug="on"
/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${build.dir}/lib"/>
<jar destfile="${build.dir}/lib/src.jar"
basedir="${build.dir}"></jar>
</target>
<target name="compiletests" depends="jar">
<mkdir dir="${build.test.dir}"/>
<javac srcdir="${test.dir}"
destdir="${build.test.dir}"
debug="on">
<classpath>
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
</classpath>
</javac>
</target>
<target name="runtest" depends="compiletests" if="junit.present">
<java fork="yes" classname="junit.textui.TestRunner" taskname="junit">
<arg value="src.test.TestHello"/>
<classpath>
<pathelement location="lib/junit-4.4.jar"/>
<pathelement location="${test.dir}"/>
<pathelement location="${build.dir}"/>
<pathelement location="${build.test.dir}"/>
<pathelement location="${java.class.path}"/>
</classpath>
</java>
</target>
</project>
解释一下runtest 任务的内容:
使用java命令运行junit.textui.TestRunner,并给该类传一个参数----我的测试类 TestHello.java , 把测试需要的类路径加上...嘿嘿,就OK啦...
贴上TestRunner的核心部分:
public class TestRunner extends BaseTestRunner ...{
private ResultPrinter fPrinter;
public static final int SUCCESS_EXIT= 0;
public static final int FAILURE_EXIT= 1;
public static final int EXCEPTION_EXIT= 2;
//.....此处省略100行.....
/** *//**
* Starts a test run. Analyzes the command line arguments and runs the given
* test suite 运行所给的test suite
*/
public TestResult start(String args[]) throws Exception ...{
String testCase= "";
String method= "";
boolean wait= false;
for (int i= 0; i < args.length; i++) ...{
if (args[i].equals("-wait"))
wait= true;
else if (args[i].equals("-c"))
testCase= extractClassName(args[++i]);
else if (args[i].equals("-m")) ...{
String arg= args[++i];
int lastIndex= arg.lastIndexOf('.');
testCase= arg.substring(0, lastIndex);
method= arg.substring(lastIndex + 1);
} else if (args[i].equals("-v"))
System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma");
else
testCase= args[i];
}
if (testCase.equals(""))
throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
try ...{
if (!method.equals(""))
return runSingleMethod(testCase, method, wait);
Test suite= getTest(testCase);
return doRun(suite, wait);
} catch (Exception e) ...{
throw new Exception("Could not create and run test suite: " + e);
}
}
/** *//**
*运行某个test 方法
*/
public TestResult doRun(Test suite, boolean wait) ...{
TestResult result= createTestResult();
result.addListener(fPrinter);
long startTime= System.currentTimeMillis();
suite.run(result);
long endTime= System.currentTimeMillis();
long runTime= endTime-startTime;
fPrinter.print(result, runTime);
pause(wait);
return result;
}
/** *//**
* 主类入口
*/
public static void main(String args[]) ...{
TestRunner aTestRunner= new TestRunner();
try ...{
TestResult r= aTestRunner.start(args);
if (!r.wasSuccessful())
System.exit(FAILURE_EXIT);
System.exit(SUCCESS_EXIT);
} catch(Exception e) ...{
System.err.println(e.getMessage());
System.exit(EXCEPTION_EXIT);
}
}
}
private ResultPrinter fPrinter;
public static final int SUCCESS_EXIT= 0;
public static final int FAILURE_EXIT= 1;
public static final int EXCEPTION_EXIT= 2;
//.....此处省略100行.....
/** *//**
* Starts a test run. Analyzes the command line arguments and runs the given
* test suite 运行所给的test suite
*/
public TestResult start(String args[]) throws Exception ...{
String testCase= "";
String method= "";
boolean wait= false;
for (int i= 0; i < args.length; i++) ...{
if (args[i].equals("-wait"))
wait= true;
else if (args[i].equals("-c"))
testCase= extractClassName(args[++i]);
else if (args[i].equals("-m")) ...{
String arg= args[++i];
int lastIndex= arg.lastIndexOf('.');
testCase= arg.substring(0, lastIndex);
method= arg.substring(lastIndex + 1);
} else if (args[i].equals("-v"))
System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma");
else
testCase= args[i];
}
if (testCase.equals(""))
throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
try ...{
if (!method.equals(""))
return runSingleMethod(testCase, method, wait);
Test suite= getTest(testCase);
return doRun(suite, wait);
} catch (Exception e) ...{
throw new Exception("Could not create and run test suite: " + e);
}
}
/** *//**
*运行某个test 方法
*/
public TestResult doRun(Test suite, boolean wait) ...{
TestResult result= createTestResult();
result.addListener(fPrinter);
long startTime= System.currentTimeMillis();
suite.run(result);
long endTime= System.currentTimeMillis();
long runTime= endTime-startTime;
fPrinter.print(result, runTime);
pause(wait);
return result;
}
/** *//**
* 主类入口
*/
public static void main(String args[]) ...{
TestRunner aTestRunner= new TestRunner();
try ...{
TestResult r= aTestRunner.start(args);
if (!r.wasSuccessful())
System.exit(FAILURE_EXIT);
System.exit(SUCCESS_EXIT);
} catch(Exception e) ...{
System.err.println(e.getMessage());
System.exit(EXCEPTION_EXIT);
}
}
}
--------------- 遇到的问题 ------------------
junit4可以使用@来定义测试方法,而以前的版本则是通过名字testXX来确定测试方法,
这就导致 testRunner不能识别@标记的方法....解决方法正在寻找中..
- ant+junit实现TDD,自动测试
- JUnit+ANT自动测试实验
- 使用Ant+JUnit+Cobertura来实现代码覆盖自动测试
- ant结合junit进行软件自动测试
- 用Ant自动测试JUnit(Ant测试自动化--上)
- ant集成junit自动测试的build.xml标准模板
- Ant 使用Junit自动测试要注意的问题
- ANT+JUnit自动化测试
- ant junit 单个测试
- ant+junit 测试一
- ant + junit 接口测试
- Ant与Junit配合来实现自动化测试
- ant+junit 自动生成测试报告
- Ant+junit的测试自动化
- Ant+junit的测试自动化
- Ant+junit的测试自动化
- Ant+junit的测试自动化
- 使用ant进行junit测试
- 用ASP实现在线压缩与解压缩
- 读易[18]·企业组织结构剖析(坤)
- 查询数据库中,表结构的详细信息
- 【帮帮】移动互联网的“浑水”
- ::细细品味ASP.NET (三)::
- ant+junit实现TDD,自动测试
- 先进先出的还款处理触发器
- 移动互联网人丁凋零的苦果早已种下
- 从digg在新媒体中的角色谈如何定义新媒体
- 避免把判断处理放入 WHERE 条件
- 【口碑】“常识”何时会自动传播
- 数据库管理员DBA
- 通过作业,定时同步两个数据库
- 爱帮网“搜索+社区”就地展开