您的位置:首页 > 其它

软件测试总结--未完待续中

2012-05-28 19:49 211 查看
首先,看一个非常简单的例子,这是一个求最值的函数:

public class Largest {
	
  public Largest() {
  }
     public static int largest(int[] list){
    	 if(list.length==0)
    	   	 throw new RuntimeException("Empty list");

     int index,max=Integer.MIN_VALUE;
     for(index=0;index<list.length;index++){
       if(list[index]>max){
         max=list[index];
       }
     }
     return max;
     
   }
  
}




下面来看看如何对他进行测试:

import junit.framework.TestCase;

public class LargestTest extends TestCase {

	 public void testSimple(){
		    assertEquals(9,Largest.largest(new int[]{9,8,7})); 
		  }
	 
	 public void testOrder1(){
	     assertEquals(9,Largest.largest(new int[]{7,9,8}));
	   }
	   
	 public void testOrder2(){
	    assertEquals(9,Largest.largest(new int[]{7,8,9}));
	  }
	 
	 public void testDups(){
	     assertEquals(9,Largest.largest(new int[]{9,7,9,8}));
	   }
	   
	  public void testOne(){
	      assertEquals(1,Largest.largest(new int[]{1}));
	    }
		
	  public void testNegative(){
		  int [] negList=new int[]{-9,-8,-7};
		  assertEquals(-7,Largest.largest(negList));
		}
	  
	  public void testEmpty(){
		    try{
		      Largest.largest(new int[]{});
		      fail("Shoud have thrown an exception");
		    }
		    catch(RuntimeException e){
		      assertTrue(true);
		    }
		  

		    
		}
	  
	 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: