惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
L
LangChain Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
I
Intezer
NISL@THU
NISL@THU
Jina AI
Jina AI
G
GRAHAM CLULEY
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
IT之家
IT之家
Security Latest
Security Latest
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
K
Kaspersky official blog
S
Securelist
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss

博客园 - Mark Jiao

调试Java程序持续占cpu问题 Freemarker notes | Freemarker学习笔记 Spring Struts Hibernate trouble shooting | 一些问题的记载 MySQL 性能调优之查询优化 Tomcat性能调优 | Tomcat Performance Tuning Tomcat配置、管理和问题解决 | Tomcat Configuration, Manangement and Trouble Shooting SWT Browser & XULRunner Windows应用安装制作工具调查报告 CentOS Configuration | CentOS配置 命令行加载IE ActiveX插件 Java那些事儿 互联网技术要考虑的事情 Git usage | Git 使用 Introduce products I worked on | 介绍一下我做过的产品 PHPUnit manual note | PHPUnit手册笔记 PHP manual notes | PHP手册笔记 Globalization, Localization, Internationalization and Translation HTTP 超文本传输协议 Design Patter
Parameterized Testing | 参数化测试
Mark Jiao · 2014-07-29 · via 博客园 - Mark Jiao

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

posted on 2014-07-29 13:54  Mark Jiao  阅读(1408)  评论()    收藏  举报