JUnit中的参数化测试运行器

我们都有书面的单元测试,其中在一个测试中针对不同的可能的输入输出组合进行测试。 让我们以一个简单的斐波那契数列为例来看看它是如何完成的。

以下代码针对提到的元素数量计算斐波那契数列:

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Fibonacci{

  public List<Integer> getFiboSeries(int numberOfElements) {
    List<Integer> fiboSeries = new ArrayList<>(numberOfElements);
    for (int i = 0; i < numberOfElements; i++) {
      //First 2 elements are 1,1
      if (i == 0 || i == 1) {
        fiboSeries.add(i, 1);
      } else {
        int firstPrev = fiboSeries.get(i - 2);
        int secondPrev = fiboSeries.get(i - 1);
        int fiboElement = firstPrev + secondPrev;
        fiboSeries.add(i, fiboElement);
      }
    }
    return fiboSeries;
  }

}

让我们看看使用多个输入值测试上述代码的常规方法

import java.util.List;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;

public class FibonacciCachedTest {

  /**
   * Test of getFiboSeries method, of class Fibonacci.
   */
  @Test
  public void testGetFiboSeries() {
    System.out.println("getFiboSeries");
    int numberOfElements = 5;
    Fibonacci instance = new Fibonacci();
    List<Integer> expResult = Arrays.asList(1, 1, 2, 3, 5);
    List<Integer> result = instance.getFiboSeries(numberOfElements);
    assertEquals(expResult, result);

    numberOfElements = 10;
    expResult = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55);
    result = instance.getFiboSeries(numberOfElements);
    assertEquals(expResult, result);

  }
}

因此,我们已经能够测试2个输入,想象将以上内容扩展为更多的输入吗? 测试代码中不必要的膨胀。

JUnit提供了另一个名为Parameterized Runner的Runner,它公开了一个带有@Parameters注释的静态方法。 必须实现此方法以返回输入和预期的输出集合,这些集合将用于运行类中定义的测试。 让我们看一下执行此操作的代码:

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class ParametrizedFiboTest {

  private final int number;
  private final List<Integer> values;

  public ParametrizedFiboTest(FiboInput input) {
    this.number = input.number;
    this.values = input.values;
  }

  @Parameterized.Parameters
  public static Collection<Object[]> fiboData() {
    return Arrays.asList(new Object[][]{
      {new FiboInput(1, Arrays.asList(1))},
      {new FiboInput(2, Arrays.asList(1, 1))},
      {new FiboInput(3, Arrays.asList(1, 1, 2))},
      {new FiboInput(4, Arrays.asList(1, 1, 2, 3))},
      {new FiboInput(5, Arrays.asList(1, 1, 2, 3, 5))},
      {new FiboInput(6, Arrays.asList(1, 1, 2, 3, 5, 8))}
    });
  }

  @Test
  public void testGetFiboSeries() {
    FibonacciUncached instance = new FibonacciUncached();
    List<Integer> result = instance.getFiboSeries(this.number);
    assertEquals(this.values, result);
  }

}

class FiboInput {

  public int number;
  public List<Integer> values;

  public FiboInput(int number, List<Integer> values) {
    this.number = number;
    this.values = values;
  }
}

这样,我们只需要在fiboData()方法中添加一个新的输入和预期的输出就可以了!

翻译自: https://www.javacodegeeks.com/2014/08/parameterized-test-runner-in-junit.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Java 参数化测试,JUnit 提供了参数化测试的功能,可以让我们更方便地编写测试用例。在 JUnit 参数化测试使用 @RunWith(Parameterized.class) 注解标记测试类,并使用 @Parameter 注解标记测试方法每个参数的值。具体步骤如下: 1. 创建一个测试类,并使用 @RunWith(Parameterized.class) 注解标记该类。 2. 声明一个静态方法,该方法返回一个集合,集合的每个元素都是一个数组,表示测试方法的参数。 3. 在测试类的构造方法,使用 @Parameter 注解标记测试方法每个参数的值。 4. 编写测试方法,使用测试方法的参数运行测试用例。 例如,下面是一个简单的参数化测试示例: ``` @RunWith(Parameterized.class) public class CalculatorTest { private int a; private int b; private int expected; public CalculatorTest(int a, int b, int expected) { this.a = a; this.b = b; this.expected = expected; } @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { 1, 2, 3 }, { 3, 4, 7 }, { 5, 6, 11 } }); } @Test public void testAdd() { Calculator calculator = new Calculator(); int result = calculator.add(a, b); assertEquals(expected, result); } } ``` 在上面的示例,我们使用 @Parameters 注解标记了一个静态方法 data(),该方法返回一个集合,集合包含了每个测试用例的参数值。在测试类的构造方法,使用 @Parameter 注解标记了测试方法的每个参数,这些参数会在测试方法被调用时传递给测试方法。在测试方法,我们使用传递过来的参数运行测试用例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值