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");
    }

}

 

 

<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>

 

解释一下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);
        }

    }





}

 

---------------   遇到的问题   ------------------

junit4可以使用@来定义测试方法,而以前的版本则是通过名字testXX来确定测试方法,

这就导致 testRunner不能识别@标记的方法....解决方法正在寻找中..

原创粉丝点击