JUnitParams参数化单元测试使用详解

    JUnitParams(github地址:https://github.com/Pragmatists/JUnitParams)作为一个开源的单元测试框架,提供了参数化测试,Coder不需要通过构造器来设置参数,JUnitParams可以由测试方法提供参数,减少了代码量。接下来咱们就一探究竟。


      1.实战:

       1.1 maven依赖:           

    <dependencies>
    <dependency>
        <groupId>pl.pragmatists</groupId>
        <artifactId>JUnitParams</artifactId>
        <version>1.0.5</version>
        <scope>test</scope>
    </dependency>
    </dependencies>
      注释:不使用maven的用户可以在maven仓库中下载jar包


         1.2 Student.java

           

/**
 * Created by zhangzh on 2017/2/20.
 */
public class Student {

    private String name;

    private String studentNo;

    private int age;

    private String schoolName;

    public Student(int age) {
        this.age = age;
    }

    public boolean isHighStudent() {
        return age >= 15 && age < 25;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStudentNo() {
        return studentNo;
    }

    public void setStudentNo(String studentNo) {
        this.studentNo = studentNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", studentNo='" + studentNo + '\'' +
                ", age=" + age +
                ", schoolName='" + schoolName + '\'' +
                '}';
    }
}

         1.3 测试类:JUnitParamsTest.java

     

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by zhangzh on 2017/2/23.
 */
@RunWith(JUnitParamsRunner.class)
public class JUnitParamsTest {

    @Test
    @Parameters({
            "3, false",
            "20, true",
            "29, false"})
    public void testHighStudent(int age, boolean valid) {

        System.out.println("age=" + age +",valid=" + valid);

        Assert.assertEquals(new Student(age).isHighStudent(), valid);

    }

    @Test
    @Parameters(method = "ageValues")
    public void studentIsHighStudent(int age, boolean valid) throws Exception {

        System.out.println("age=" + age +",valid=" + valid);

        Assert.assertEquals(new Student(age).isHighStudent(), valid);
    }

    private Object[] ageValues() {
        return new Object[]{
                new Object[]{13, false},
                new Object[]{17, true},
                new Object[]{35, false}
        };
    }


    /**
     * 默认约定,当不指定@Parameters中的method方法时,JUnitParams默认查找“parametersFor + 自己做注解的方法名称”作为@Parameters的参数方法
     *      例如,@Parameters注解方法为:personIsAdult_2,则其默认关联的方法名称为:parametersForPersonIsAdult_2
      */




    @Test
    @Parameters
    public void studentIsHighStudent_2(int age, boolean valid) throws Exception {
        System.out.println("age=" + age +",valid=" + valid);

        Assert.assertEquals(new Student(age).isHighStudent(), valid);
    }

    private Object[] parametersForStudentIsHighStudent_2() {
        return new Object[]{
                new Object[]{13, false},
                new Object[]{17, true},
                new Object[]{36, false}
        };
    }


    /**
     *  变量传入对象
     */
    @Test
    @Parameters
    public void studentIsHighStudent_3(Student student, boolean valid) throws Exception {
        System.out.println("age=" + student.getAge() +",valid=" + valid);
        Assert.assertEquals(student.isHighStudent(), valid);
    }

    private Object[] parametersForStudentIsHighStudent_3() {
        return new Object[]{
                new Object[]{new Student(13), false},
                new Object[]{new Student(17), true},
                new Object[]{new Student(45), false}
        };
    }


}

 看,简单吧!


      参考资料:1.JunitParams的github地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值