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

  • 相关阅读:
    CSS学习笔记07 盒子模型
    [Android]AndFix使用说明
    [Android]自定义控件LoadMoreRecyclerView
    [算法]Plus One
    [Android]android Service后台防杀
    [Android]android studio预览视图时报错
    [算法]删除无序单链表中值重复出现的节点
    [算法] 将单链表的每K个节点之间逆序
    [Android]热修复框架AndFix测试说明
    [算法]单链表的选择排序
  • 原文地址:https://www.cnblogs.com/markjiao/p/3875363.html
Copyright © 2011-2022 走看看