Junit4测试

发表于:2008-3-13 16:20

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

        完整的循环测试代码如下:
Java代码
import static org.junit.Assert.*;  
 
import java.util.Arrays;  
import java.util.Collection;  
 
import org.junit.AfterClass;  
import org.junit.BeforeClass;  
import org.junit.Ignore;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
import org.junit.runners.Parameterized;  
import org.junit.runners.Parameterized.Parameters;  
 
/** 
 * @author bulargy.j.bai 
 * @创建时间:Mar 11, 2008 
 * @描述: 
 */ 
@RunWith(Parameterized.class)  
public class MathTest {  
    int faciend;  
    int multiplicator;  
    int result;  
 
    public MathTest(int faciend, int multiplicator, int result) {  
         this.faciend = faciend;  
         this.multiplicator = multiplicator;  
         this.result = result;  
    }  
    /** 
     * @throws java.lang.Exception 
     */ 
    @BeforeClass 
    public static void setUpBeforeClass() throws Exception {  
    }  
 
    /** 
     * @throws java.lang.Exception 
     */ 
    @AfterClass 
    public static void tearDownAfterClass() throws Exception {  
    }  
 
    /** 
     * Test method for {@link org.bj.util.Math#divide(int, int)}. 
     */ 
    @Test(expected=ArithmeticException.class)  
    public void testDivide() {  
        assertEquals(3,Math.divide(9,3));  
        assertEquals(3,Math.divide(10,3));  
        Math.divide(10,0);//除数不能为0,会抛出异常  
 
    }  
 
    /** 
     * Test method for {@link org.bj.util.Math#multiple(int, int)}. 
     */ 
    //@Ignore("忽略乘法测试")  
    @Test 
    public void testMultiple() {  
        assertEquals(result,Math.multiple(faciend,multiplicator));  
    }  
      
    @Parameters 
    public static Collection multipleValues() {  
     return Arrays.asList(new Object[][] {  
        {3, 2, 6 },  
        {4, 3, 12 },  
        {21, 5, 105 },  
        {11, 22, 242 },  
        {8, 9, 72 }});  
    }  
 

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * @author bulargy.j.bai
 * @创建时间:Mar 11, 2008
 * @描述:
 */
@RunWith(Parameterized.class)
public class MathTest {
 int faciend;
 int multiplicator;
 int result;

 public MathTest(int faciend, int multiplicator, int result) {
   this.faciend = faciend;
   this.multiplicator = multiplicator;
   this.result = result;
 }
 /**
  * @throws java.lang.Exception
  */
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 /**
  * @throws java.lang.Exception
  */
 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 /**
  * Test method for {@link org.bj.util.Math#divide(int, int)}.
  */
 @Test(expected=ArithmeticException.class)
 public void testDivide() {
  assertEquals(3,Math.divide(9,3));
  assertEquals(3,Math.divide(10,3));
  Math.divide(10,0);//除数不能为0,会抛出异常

 }

 /**
  * Test method for {@link org.bj.util.Math#multiple(int, int)}.
  */
 //@Ignore("忽略乘法测试")
 @Test
 public void testMultiple() {
  assertEquals(result,Math.multiple(faciend,multiplicator));
 }
 
 @Parameters
 public static Collection multipleValues() {
  return Arrays.asList(new Object[][] {
        {3, 2, 6 },
        {4, 3, 12 },
        {21, 5, 105 },
        {11, 22, 242 },
        {8, 9, 72 }});
 }

}
        OK,大功告成。测试看看吧,测试类跑了5次~~。

        大概就这么多体会了,总得来说JUnit4以后测试还是很方便的,顺便这个是仅仅是为了做例子,实际使用中由于JUnit4不再受命名的限制,所以应该划分更细粒度的测试来完成,一个方法的正确,异常,错误及边界数据完全可以分开来写测试方法。由于大部分情况资源只用加载和释放一次就足够,大大提高的测试的速度,再也不会有以前那样点开测试然后去泡咖啡的情况出现了~~呵呵~~

33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号