junit学习(三)--junit4使用

1.在junit4中,通过@Before注解实现与junit3.8中setUp()方法同样的功能,通过@After注解来实现与junit3.8中tearDown方法相同的功能,通过@Test注解来public void 的无参数方法来说明这个方法是测试方法。注意此时方法名可以任意取,但最好还是和junit3.8保持一致。

 

2.junit4中,可以使用@BeforeClass与@AfterClass注解修饰一个public static void no-arg 的方法,这样@BeforeClass注解所修饰的方法会在所有测试方法执行之前执行,@AfterClass注解所修饰的方法会在所有测试方法执行之后前执行,且只会执行一次。

 

3.@Test中有属性expected和timeout。expected希望抛出的异常 ,timeout设置测试执行不得超过的时间。如:Test(timeout = 600)时间不能超过600毫秒

  • package com.junit;
  • import org.junit.Before;
    import org.junit.Test;
  • public class CalculatorTest {
     private Calculator cal;
     @Before
     public void init()
     {
      cal = new Calculator();
     }
     @Test(timeout = 100,expected = Exception.class)
     public void testDivide() throws Exception
     {
      cal.divide(1, 0);
     }
    }

4.将多个测试整合在一起运行,运用注解@RunWith(Suite.class)和@Suite.SuiteClasses({})

  • package com.junit;
  • import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
  • @RunWith(Suite.class)
    @Suite.SuiteClasses({CalculatorTest.class,OtherTest.class})
    public class TestAll {
     /*
      * 类中可是没都没有
      */
    }

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值