JUnit4 参数化测试( Parameterized tests)

13 篇文章 0 订阅
6 篇文章 0 订阅

@RunWith

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

  要进行参数化测试,需要在类上面指定如下的运行器:

@RunWith (Parameterized.class)

然后,在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection。


Junit4 中通过 Parameterized 运行器实现参数化测试。

当执行参数化测试类时,实例的测试方法和测试数据元素将在测试示例创建时交叉连接到一起。

下面是测试菲波那切数的测试方法:


   
   
  1. import static org.junit.Assert.assertEquals;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.Parameterized;
  7. import org.junit.runners.Parameterized.Parameters;
  8. @RunWith(Parameterized.class)
  9. public class FibonacciTest {
  10. @Parameters
  11. public static Collection<Object[]> data() {
  12. return Arrays.asList( new Object[][] {
  13. { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
  14. });
  15. }
  16. private int fInput;
  17. private int fExpected;
  18. public FibonacciTest(int input, int expected) {
  19. fInput= input;
  20. fExpected= expected;
  21. }
  22. @Test
  23. public void test() {
  24. assertEquals(fExpected, Fibonacci.compute(fInput));
  25. }
  26. }

   
   
  1. public class Fibonacci {
  2. public static int compute(int n) {
  3. int result = 0;
  4. if (n <= 1) {
  5. result = n;
  6. } else {
  7. result = compute(n - 1) + compute(n - 2);
  8. }
  9. return result;
  10. }
  11. }

每个FibonacciTest类的示例都将通过两个参数的构造器来创建。而这两个参数将通过带有@Parameters注解的方法来获取。

除了构造器注入之外,@Parameters注解支持属性注入


   
   
  1. import static org.junit.Assert.assertEquals;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.Parameterized;
  7. import org.junit.runners.Parameterized.Parameter;
  8. import org.junit.runners.Parameterized.Parameters;
  9. @RunWith(Parameterized.class)
  10. public class FibonacciTest {
  11. @Parameters
  12. public static Collection<Object[]> data() {
  13. return Arrays.asList( new Object[][] {
  14. { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
  15. });
  16. }
  17. @Parameter // first data value (0) is default
  18. public /* 不能为 private */ int fInput;
  19. @Parameter( 1)
  20. public /* 不能为 private */ int fExpected;
  21. @Test
  22. public void test() {
  23. assertEquals(fExpected, Fibonacci.compute(fInput));
  24. }
  25. }
  26. public class Fibonacci {
  27. ...
  28. }
注意,目前只支持公共属性的注入


单个参数的测试

如果你的测试只需要单个参数,则不需要将其包装成数组。这种情况下可以提供一个迭代器或对象数组。


   
   
  1. @Parameters
  2. public static Iterable<? extends Object> data() {
  3. return Arrays.asList( "first test", "second test");
  4. }


   
   
  1. @Parameters
  2. public static Object[] data() {
  3. return new Object[] { "first test", "second test" };
  4. }

识别每个测试用例

为了更容易地区分参数化测试中每个测试用例,你可以在@Parameters 注解上提供一个名称。

此名称可以包含占位符,该占位符在运行时将会被替换。

  • {index}: 当前参数的索引
  • {0}, {1}, …: 第一个参数,第二个参数等,参数值. 注意:单引号 ' 应该被转义成两个单引号 ''.

例子


   
   
  1. import static org.junit.Assert.assertEquals;
  2. import java.util.Arrays;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.junit.runners.Parameterized;
  6. import org.junit.runners.Parameterized.Parameters;
  7. @RunWith(Parameterized.class)
  8. public class FibonacciTest {
  9. @Parameters(name = "{index}: fib({0})={1}")
  10. public static Iterable<Object[]> data() {
  11. return Arrays.asList( new Object[][] {
  12. { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
  13. });
  14. }
  15. private int input;
  16. private int expected;
  17. public FibonacciTest(int input, int expected) {
  18. this.input = input;
  19. this.expected = expected;
  20. }
  21. @Test
  22. public void test() {
  23. assertEquals(expected, Fibonacci.compute(input));
  24. }
  25. }
  26. public class Fibonacci {
  27. ...
  28. }


上面这个例子,参数化 运行器 创建如 [3: fib(3)=2] 这种名称。如果你没有指定名称,默认使用当前参数的索引。


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值