zoukankan      html  css  js  c++  java
  • Parameterized Testing | 参数化测试

    JUnit Parameterized tests      

    https://github.com/junit-team/junit/wiki/Parameterized-tests

     1 package org.ut.parameter;
     2 public class Fibonacci {
     3     public static void main(String[] args) {
     4         int nums = compute(6);  
     5         System.out.println(nums);
     6     }
     7     //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
     8     public static int compute(int n) {
     9         if(n==0) return 0;   
    10         if(n<=1) return 1;   
    11         return compute(n-1)+compute(n-2);
    12     }
    13 }
    Source code
    package org.ut.parameter;
    import static org.junit.Assert.*;
    import java.util.Arrays;
    import java.util.Collection;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(Parameterized.class)
    public class FibonacciTest {
        @Parameters
        public static Collection<Object[]> 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;
        /*
         * Each instance of FibonacciTest will be constructed using the two-argument constructor and
         * the data values in the @Parameters method.
         */
        public FibonacciTest(int input, int expected) {
            fInput= input;
            fExpected= expected;
        }
    
        @Test
        public void test() {
            assertEquals(fExpected, Fibonacci.compute(fInput));
        }
    }
    Parameterized test type 1
     1 package org.ut.parameter;
     2 import static org.junit.Assert.*;
     3 import java.util.Arrays;
     4 import java.util.Collection;
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.junit.runners.Parameterized;
     8 import org.junit.runners.Parameterized.Parameter;
     9 import org.junit.runners.Parameterized.Parameters;
    10 
    11 @RunWith(Parameterized.class)
    12 public class FibonacciTest2 {
    13     /*
    14      * Identify Individual test cases
    15      * @Parameters(name = "{index}: fib({0})={1}")
    16      */
    17     @Parameters(name = "{index}: fib[{0}]={1}")
    18     public static Collection<Object[]> data() {
    19         return Arrays.asList(new Object[][] {
    20 
    21                  { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },{ 6, 8 }  
    22            });
    23     }
    24     /*
    25      * It is also possible to inject data values directly into fields without
    26      * needing a constructor using the @Parameter annotation
    27      */
    28     @Parameter // first data value (0) is default
    29     public /* NOT private */ int fInput;
    30 
    31     @Parameter(value = 1)
    32     public /* NOT private */ int fExpected;
    33 
    34     @Test
    35     public void test() {
    36         assertEquals(fExpected, Fibonacci.compute(fInput));
    37     }
    38 }
    Parameterized test type 2

    @Parameters(name = "{index}: fib[{0}]={1}")

    @Parameters(name = "{index}: fib({0})={1}")

    See https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512

  • 相关阅读:
    rn 安卓开发报错
    rn 打包如果真机或者模拟机打包编译报错
    fluter 导航以及其他
    fluter 相关布局组件
    fluter 万事皆组件。常用组件
    rn开发问题简要
    app配置环境和打包方式以及其他问题
    小程序大概的开发流程
    ant-design-pro 开发 基于react 框架涉及到技术你的本地环境需要安装 yarn、node 和 git。我们的技术栈基于 ES2015+、React、UmiJS、dva、g2 和 antd,
    unity打包的webgl结合react使用
  • 原文地址:https://www.cnblogs.com/markjiao/p/3875363.html
Copyright © 2011-2022 走看看