在JUnit中,可以同时使用@RunWith 和 @parameter 注解来为单元测试传递参数。

注意:

在Eclipse中因为版本问题,可能无法使用@parameters(name = "xxx"),详见解决办法(文章的最后部分: IDE Bug - Eclipse):

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

1. MathUtils – 参数化测试

查看一个简单的 add 方法。

MathUtils.java

package com.yiibai.match.utils;

public class MathUtils {

	public static int add(int a, int b) {
return a + b;
} }

这里有一个为上面的JUnit测试类,阅读不言自明的注释。

MathUtilsTest.java

package com.yiibai.match.utils;

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(value = Parameterized.class)
public class MathUtilsTest { private int numberA;
private int numberB;
private int expected; //parameters pass via this constructor
public MathUtilsTest(int numberA, int numberB, int expected) {
this.numberA = numberA;
this.numberB = numberB;
this.expected = expected;
} //Declares parameters here
@Parameters(name = "{index}: add({0}+{1})={2}")
public static Iterable<Object[]> data1() {
return Arrays.asList(new Object[][] {
{ 1, 1, 2 },
{ 2, 2, 4 },
{ 8, 2, 10 },
{ 4, 5, 9 }
});
} @Test
public void test_add() {
assertEquals(expected,MathUtils.add(numberA, numberB));
} }

什么是 {0}, {1} 和 {2}?
如果参数是 “{ 3, 4, 7 }”, 那么 {0} = 3, {1} = 4, {2} = 7. 请参考以下的输出:

2. DomainUtils – 参数化测试

查看域名验证程序。

DomainUtils.java

package com.yiibai.regex.utils;

import java.util.regex.Pattern;

public class DomainUtils {

	private static Pattern pDomainName;

	private static final String DOMAIN_NAME_PATTERN = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$";

	static {
pDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
} //is this a valid domain name?
public static boolean isValidDomainName(String domainName) {
return pDomainName.matcher(domainName).find();
} }

这里有一个 JUnit 测试上面的类。

DomainUtilsTest.java

package com.yiibai.regex.utils;

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(value = Parameterized.class)
public class DomainUtilsTest { private String domain;
private boolean expected; public DomainUtilsTest(String domain, boolean expected) {
this.domain = domain;
this.expected = expected;
} @Parameters(name= "{index}: isValid({0})={1}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "google.com", true },
{ "yiibai.com", true },
{ "-yiibai.com", false },
{ "yiibai-.com", false },
{ "3423kjk", false },
{ "mk#$kdo.com", false }
}
);
} @Test
public void test_validDomains() {
assertEquals(expected,DomainUtils.isValidDomainName(domain));
} }

输出结果:

完成

JUnit4参数化测试实例的更多相关文章

  1. JMeter学习-026-JMeter 分布式(远程)参数化测试实例

    以前文所述对文章详情的HTTP请求进行性能测试为例.日常实际场景中,不可能所有的人都在同时访问一篇文章,而是多人访问不同的文章,因而需要对文章编号进行参数化,以更好的模拟日常的性能测试场景.同时,因文 ...

  2. Junit4参数化测试实现程序与用例数据分离

    http://touchfu.iteye.com/blog/732930 现状:你是不是还在为自己的TestCase代码杂乱无章而苦恼,咎其根本还在于针对不同的用例,输入参数和mock信息的组装全部作 ...

  3. 用JUnit4进行参数化测试

    参数化测试是一个JUnit 3不具备的功能. 基本使用方法 @RunWith 当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner) ...

  4. 同时使用Junit4的@Parameterized参数化测试和Spring容器

    转载:http://www.jianshu.com/p/d191fe54915f 整合Spring容器 @SpringApplicationConfiguration(classes = Applic ...

  5. Junit4进行参数化测试

    @RunWith, 当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器. 要进行参数化测 ...

  6. Google C++单元测试框架GoogleTest---值参数化测试

    值参数化测试允许您使用不同的参数测试代码,而无需编写同一测试的多个副本. 假设您为代码编写测试,然后意识到您的代码受到布尔参数的影响. TEST(MyCodeTest, TestFoo) { // A ...

  7. junit 单元测试 - 参数化测试

    junit4.x版本需要引入如下jar包: hamcrest-core-1.3.jar junit-4.12-beta-3.jar 新建一个计算器类,如下: package com.pt; publi ...

  8. Jmeter 参数化请求实例

    Jmeter 参数化请求实例 在jmeter中的请求可以参数化,其中参数化的方式有4种: 1.CSV Data Set Config 2.数据库 3.用户自定义变量 4.用jmeter中的函数获取参数 ...

  9. junit参数化测试

    在前面的junit4初体验中我就说过,junit参数化测试是一只小怪兽,只逼编码痛点,现在我们这里来整理一下. 看过我前面的那篇初体验的就会发现一个问题,我们的测试代码大量的重复了.在这里先贴出原来的 ...

随机推荐

  1. angular内置过滤器-filter

    这篇文章来讲解一下angular内置的filter过滤器. 没错,这个过滤器的名字,就叫'filter',虽然自定义过滤器也是使用module.filter()...但是不要混淆了,这个filter就 ...

  2. angular学习笔记(三十)-指令(7)-compile和link(3)

    本篇接着上一篇来讲解当指令中带有template(templateUrl)时,compile和link的执行顺序: 把上一个例子的代码再进行一些修改: 1.将level-two指令改成具有templa ...

  3. Nan-boxing技术介绍

    NaN-boxing看起来像英文翻译的“南拳”,其实它是表示一个无效的double数.NaN-boxing技术:通过一个64位的数字来表示多种数据类型的技术,它通过一个nan浮点数来保存数据,根据IE ...

  4. 每日英语:Making the Most of Your Lunch Hour

    More Americans are eating lunch at their desks or even forgoing it altogether. Is passing up a prope ...

  5. C# 去除json字符串key引号

    采用正则表达式去除: 方法 /// <summary> /// 去除json key双引号 /// </summary> /// <param name="js ...

  6. pandas的连接函数concat()函数

        pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,   keys=None, levels=No ...

  7. mysql性能优化(二)

    key_buffer_size 为了最小化磁盘的 I/O , MyISAM 存储引擎的表使用键高速缓存来缓存索引,这个键高速缓存的大小则通过 key-buffer-size 参数来设置.如果应用系统中 ...

  8. ThinkPHP使用Smarty

    ThinkPHP支持多种php模板引擎,可以根据个人需要加以配置. 第一步: 首先去Smarty官网上下载一个Smarty. 第二步: 解压压缩包,会有两个文件夹:demo和libs.打开libs文件 ...

  9. jquery开发的”天才笨笨碰“游戏

    前段时间湖南卫视的快乐大本营里有一款“天才笨笨碰”游戏非常火.这款游戏主要是考选手的声母联想词语的能力. 小篇在看完这个节目后用jquery制作了“天才笨笨碰”网页游戏.先上效果图: 游戏规则: 1. ...

  10. java好用的邮件发送

    1.action代码 // 发送邮件 String strEMAIL = Tools.readTxtFile(Const.EMAIL); //读取邮件配置 String strEM[] = strEM ...