JUnit+ANT自动测试实验

目标:
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 >


代码全部通过测试。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值