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

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
Help Net Security
Help Net Security
Jina AI
Jina AI
Security Latest
Security Latest
Y
Y Combinator Blog
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
博客园 - 司徒正美
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
D
Docker
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
B
Blog
L
LangChain Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
The Hacker News
The Hacker News
C
Check Point Blog
L
Lohrmann on Cybersecurity
V2EX - 技术
V2EX - 技术
S
Securelist
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
Latest news
Latest news
人人都是产品经理
人人都是产品经理
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
AWS News Blog
AWS News Blog
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
T
Tailwind CSS Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
宝玉的分享
宝玉的分享
O
OpenAI News

博客园 - 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)  评论()    收藏  举报