JUnit的注解、套件和参数化设置的介绍

JUnit4中的注解、测试套件和参数化设置

注解

上面博文已经介绍了如下的注解:

1、@BeforeClass,用此注解修饰的方法将在所有方法运行前被执行,是一个static方法,只执行一次。

2、@AfterClass,用此注解修饰的方法将在所有方法运行后被执行,也是一个static方法,只执行一次。

3、@Before,用此注解修饰的方法在每个test方法运行前执行

4、@After,用此注解修饰的方法在每个test方法运行后执行

5、@Test,表明此方法为测试方法。

其中,@Test有两个参数,分别为:expected 和 timeout。

下面将以例子的形式来说明这两个参数的作用。

先看expected参数

public class AnonationTest {
    @Test
    public void testDivide() {
        assertEquals(2, new Calcalate().divide(4, 0));
    }
    @Test(expected=ArithmeticException.class)
    public void testDivide1(){
        assertEquals(2, new Calcalate().divide(4, 0));
    }
}

在上面的Test中,有两个测试方法,他们的唯一区别在于,testDivide1方法中用注解@Test带有excepted参数。

运行结果如下:

从结果可以看出,testDivide是报错的,报除零错误。而testDivide1由于使用了@Test(expected=ArithmeticException.class),即预期会有算术异常会发生,即使真的有异常发生,也会通过。

@Test(expected=xxx.class),预期会发生什么,这就是此参数的作用。

下面来看 @Test的参数 timeout

timeout的作用:为测试函数设定一定的运行时间,避免死循环。

@Test(timeout=4000)
    public void testWhile(){
        while(true){
            System.out.println("run always..");
        }
    }

上面的测试函数中,就是使用了timeout参数来限时4000毫秒。如果不用它来进行限时,则testWhile这个函数就会一直运行下去。

除了上面介绍的注解之外,还有两个注解。

6、Ignore:所修饰的测试方法会被测试运行器忽略。

看如下的代码:

public class AnonationTest {
    @Test
    @Ignore
    public void testDivide() {
        assertEquals(2, new Calcalate().divide(4, 0));
    }
    @Test(expected=ArithmeticException.class)
    public void testDivide1(){
        assertEquals(2, new Calcalate().divide(4, 0));
    }
    @Test(timeout=4000)
    @Ignore
    public void testWhile(){
        while(true){
            System.out.println("run always..");
        }
    }
}

上面的代码中,有两个测试方法被注解@Ignore 修饰了,则这两个方法都会被测试运行器所忽略。

运行结果如下,红线标出来的测试方法就是被忽略的,与我们的预期一样。

7、RunWith:更改测试运行器,在介绍JUnit套件的使用中会介绍到。

JUnit4套件的使用

测试套件就是组织一组测试类一起运行的。

如何来写一个测试套件呢??

1、写一个作为测试套件的入口类,这个类中不包含任何内容。

2、用注解@RunWith更改测试运行器为:Suite.class.

3、将要测试的类作为数组传入到@Suite.SuiteClasses({})中即可。

如下是一个例子:

//定义一个套件,套件里面必须为空
    @RunWith(Suite.class)  //更改测试运行器
    //下面这个是加载运行的测试类,用数组的形式给出
    @Suite.SuiteClasses({CalculateTest.class,CalculateTest2.class,CalculateTest3.class,AnonationTest.class})
    public class SuiteTest {
        // 为空
    }

JUnit4的参数化设置

参数化设置的几个步骤如下:

第一步:更改测试运行器为RunWith(Parameterized.class)

第二步:声明变量来存放预期值和输入值

第三步:声明一个返回值为Collection的公共静态方法,并使用@Parameters来进行修饰

第四步:声明一个构造函数,并在其中为之声明变量赋值

第五步:声明测试函数

例子程序如下:

    @RunWith(Parameterized.class)   //第一步
    public class ParameterTest {
    //第二步
    private int expected;
    private int input1;
    private int input2;
    //第三步
    @Parameters
    public static Collection<Object[]> t(){
        return Arrays.asList(new Object[][]{
                {5,1,4},
                {8,2,6}
        });
    } 
    //第四步
    public ParameterTest(int expected,int input1,int input2){
        this.expected=expected;
        this.input1=input1;
        this.input2=input2;
    }
    //第五步
    @Test
    public void testAdd(){
        assertEquals(expected, new Calcalate().add(input1, input2));
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值