JUnit4:多组数据的单元测试:用JUnit4做参数化测试

 

用JUnit4进行参数化测试

  参数化测试是一个JUnit 3不具备的功能。

 

基本使用方法

  @RunWith

  当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器。

  要进行参数化测试,需要在类上面指定如下的运行器:

  @RunWith (Parameterized.class)

  然后,在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection

  

  在测试类的构造方法中为各个参数赋值,(构造方法是由JUnit调用的),最后编写测试类,它会根据参数的组数来运行测试多次

 

文档中的例子

  Class Parameterized的文档中有一个例子:

  For example, to test a Fibonacci function, write:

  

    @RunWith(Parameterized.class)
    public class FibonacciTest
    {
        @Parameters
        public static Collection data()
        {
            return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                    { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
        }

        private int fInput;
        private int fExpected;

        public FibonacciTest(int input, int expected)
        {
            fInput = input;
            fExpected = expected;
        }

        @Test
        public void test()
        {
            assertEquals(fExpected, Fibonacci.compute(fInput));
        }
    }

 

参数化测试实例

  还是以前面的Calculator类作为例子,进行参数化测试:

Calculator
package com.mengdd.junit4;

public class Calculator
{
    public int add(int a, int b)
    {
        return a + b;
    }

    public int subtract(int a, int b)
    {
        return a - b;
    }

    public int multiply(int a, int b)
    {
        return a * b;
    }

    public int divide(int a, int b) throws Exception
    {
        if (0 == b)
        {
            throw new Exception("除数不能为0");
        }
        return a / b;
    }

}

  参数化测试加法的类:

package com.mengdd.junit4;

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

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
// 指定运行器runner:使用参数化运行器来运行
public class ParametersTest
{
    private int expected;// 期待的结果值

    private int input1;// 参数1

    private int input2;// 参数2

    private Calculator calculator = null;

    @Parameters
    public static Collection prepareData()
    {
        // 测试数据
        Object[][] objects = { { 3, 1, 2 }, { -4, -1, -3 }, { 5, 2, 3 },
                { 4, -4, 8 } };
        return Arrays.asList(objects);// 将数组转换成集合返回

    }

    @Before
    public void setUp()
    {
        calculator = new Calculator();
    }

    public ParametersTest(int expected, int input1, int input2)
    {
        // 构造方法
        // JUnit会使用准备的测试数据传给构造函数
        this.expected = expected;
        this.input1 = input1;
        this.input2 = input2;
    }

    @Test
    public void testAdd()
    {
        Assert.assertEquals(this.expected,
                calculator.add(this.input1, this.input2));
    }

}

 

 

参考资料

  圣思园张龙老师视频教程。

  JUnit4 chm格式文档网盘下载链接:

  JUnit 4.0:http://pan.baidu.com/share/link?shareid=539345&uk=2701745266

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值