• 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、重复以上过程

  • 相关阅读:
    安亦行
    [SCOI2010]连续攻击游戏 二分图匹配
    [USACO11JAN]Roads and Planes G
    CF796C Bank Hacking
    括号类问题总结 持续更新
    CF1216E Numerical Sequence Hard Version 数学
    POJ3613 Cow Relays 矩阵快速幂
    倍增法求lca(最近公共祖先)
    Codeforces C. Jzzhu and Cities(dijkstra最短路)
    Codeforces B. Mouse Hunt(强连通分解缩点)
  • 原文地址:https://www.cnblogs.com/maple42/p/5505539.html
Copyright © 2020-2023  润新知