zoukankan      html  css  js  c++  java
  • Junit参数化运行

    package test.Junit;
    import Junit.Calculator;
    import org.junit.*;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    import java.util.Arrays;
    import java.util.Collection;
    
    import static org.junit.Assert.assertEquals;
    /**
     * Created by spook on 16/5/18.
     */
    
    @RunWith(value = Parameterized.class)
    public class ParameterizedTest {
        @Before
        public void before() throws Exception{
    
    }
        private double expected;
        private double valueone;
        private double valuetwo;
        @Parameters
        public static Collection<Integer[]> getTestParameters(){
            return Arrays.asList(new Integer[][]{
                    {2,1,1},
                    {3,2,1},
                    {5,3,1}
            });
    }
        public ParameterizedTest(double expected,double valueone,double valuetwo){
            this.expected=expected;
            this.valueone=valueone;
            this.valuetwo=valuetwo;
        }
        @Test
        public void sum(){
            Calculator cal = new Calculator();
            assertEquals(expected, cal.add(valueone, valuetwo),1);//第三个参数为误差,因此第三题case可以运行通过
    }
    
    }

    Junit参数化运行

    1、Junit调用静态方法 getTestParameters,接着junit为getTestParameters集合中的每个数组进行循环;

    2、然后Junit调用唯一的构造函数,如果存在多个构造函数会抛断言错误,接着用数组元素构成一系列参数调用构造函数{2,1,1}等

    3、重复以上过程

  • 相关阅读:
    软件概要设计
    Jmter进行Rabbitmq测试
    分布式锁
    实用异常
    异常集合
    本地Windows环境安装RabbitMQ Server
    MySQL千万级多表关联SQL语句调优
    mysql数据库多表关联查询的慢SQL优化
    rabbitmq详细配置
    Idea连接服务器docker并部署代码到docker实现一键启动
  • 原文地址:https://www.cnblogs.com/maple42/p/5505539.html
Copyright © 2011-2022 走看看