JUnit+ANT自动测试实验

来源:互联网 发布:学校网络限制玩游戏 编辑:程序博客网 时间:2024/05/21 23:22
目标:
1. 使用ANT自动化测试。
2. 自动化测试报告。
3. 定时执行自动化测试。(未实现。)

被测试类 DefaultCalculator.java:
package com.don.classes;

/**
 * 
@author Administrator
 *
 
*/

public class DefaultCalculator {
    
private int x, y;
    
/**
     * 
     
*/

    
public DefaultCalculator(int x, int y) {
        
this.x = x; this.y = y;
    }

    
    
public int Add() {
        
return x + y;
    }

    
    
public int Subtract() {
        
return x - y;
    }

    
    
public int Multiply(){
        
return x * y;
    }

    
    
public int Divide() {
        
return x / y;
    }


    
public int getX() {
        
return x;
    }


    
public int getY() {
        
return y;
    }


}


测试用例DefaultCalculatorTest.java:
package com.don.classes;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.don.classes.DefaultCalculator;
import java.util.ArrayList;
import java.util.List;

public class DefaultCalculatorTest {
    
private List<DefaultCalculator> calcs =  new ArrayList<DefaultCalculator>();

    @Before
    
public void setUp() throws Exception {
         calcs.add(
new DefaultCalculator(2040));
         calcs.add(
new DefaultCalculator(-10-10));
         calcs.add(
new DefaultCalculator(40-10));
    }


    @After
    
public void tearDown() throws Exception {
        calcs 
= null;
    }


    @Test
    
public void testDefaultCalculator() {
        assertEquals(
10, calcs.get(0).getX());
        assertEquals(
40, calcs.get(0).getY());
        assertEquals(
-10, calcs.get(1).getX());
        assertEquals(
-10, calcs.get(1).getY());
        assertEquals(
40, calcs.get(2).getX());
        assertEquals(
-10, calcs.get(2).getY());
    }


    @Test
    
public void testAdd() {
        assertEquals(
50, calcs.get(0).Add());
        assertEquals(
-20, calcs.get(1).Add());
        assertEquals(
30, calcs.get(2).Add());
    }


    @Test
    
public void testSubtract() {
        assertEquals(
-30, calcs.get(0).Subtract());
        assertEquals(
0, calcs.get(1).Subtract());
        assertEquals(
50, calcs.get(2).Subtract());
    }


    @Test
    
public void testMultiply() {
        assertEquals(
400, calcs.get(0).Multiply());
        assertEquals(
100, calcs.get(1).Multiply());
        assertEquals(
-400, calcs.get(2).Multiply());
    }


    @Test
    
public void testDivide() {
        assertEquals(
0, calcs.get(0).Divide());
        assertEquals(
1, calcs.get(1).Divide());
        assertEquals(
-4, calcs.get(2).Divide());
    }


}


Ant脚本:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     2008-4-27 上午11:45:00                                                        

     project        : antjunit    
     description    : 使用Ant自带的junit,jreport任务来测试DefaultCalculator类
                   
     Administrator    :李凯                                                               
     ====================================================================== 
-->
<project name="project" default="jreport">
    
<description>
            测试,自动生成DefaultCalculator类的测试报告。
    
</description>

    
<property name="src" value="src"/>
    
<property name="test" value="test" />
    
<property name="bin" value="bin"/>
    
<property name="lib" value="lib"/>
    
<property name="report" value="report"/>


    
<!-- ================================= 
          target: compile              
         ================================= 
-->
    
<target name="compile" description="--> 编译源码">
        
<javac destdir="${bin}">
            
<src path="${src}" />
            
<src path="${test}" />
        
</javac>        
    
</target>

    
<!-- ================================= 
          target: junit              
         ================================= 
-->
    
<target name="junit" depends="compile" description="--> 执行junit任务">
        
<mkdir dir="${report}"/>

        
<junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="yes">
            
<classpath>
                
<pathelement path="${bin}"/>
                
<fileset dir="${lib}">
                    
<include name="**/*.jar"/>
                
</fileset>
            
</classpath>
            
<formatter type="plain" usefile="false"/>
            
<formatter type="xml"/>
            
<test name="com.don.classes.DefaultCalculatorTest" todir="${report}" />
        
</junit> 
    
</target>
    
    
<!-- ================================= 
          target: jreport              
         ================================= 
-->
    
<target name="jreport" depends="junit" description="--> 自动生成Report">
        
<mkdir dir="${report}/html"/>
        
        
<junitreport todir="${report}/html">
            
<fileset dir="${report}">
                
<include name="TEST-*.xml"/>
            
</fileset>
            
            
<report todir="${report}/html"/>
        
</junitreport>
    
</target>
</project>


代码全部通过测试。