【问题标题】:How to run JUnit 4 Data driven test with multiple data如何使用多个数据运行 JUnit 4 数据驱动测试
【发布时间】:2019-09-22 11:41:35
【问题描述】:

我正在尝试为具有两个重载方法的类编写 junit 测试。这是我的测试代码

public class Solution {
    public static String getDurationString(int minute, int seconds) {
        if (minute < 0) {
            return "Invalid Value";
        } else if (seconds < 0 || seconds > 59) {
            return "Invalid Value";
        } else {
            int hours = minute / 60;
            minute %= 60;
            return getTimeString(hours) + "h " + getTimeString(minute) + "m " + seconds + "s";
        }
    }

    public static String getDurationString(int seconds) {
        if (seconds < 0) {
            return "Invalid Value";
        }

        int minutes = seconds / 60;
        seconds %= 60;
        int hours = minutes / 60;
        minutes %= 60;

        return getTimeString(hours) + "h " + getTimeString(minutes) + "m " + seconds + "s";
    }

    //returns value in xx format
    private static String getTimeString(int val) {
        if (val < 10) {
            return "0" + val;
        }
        return "" + val;
    }
}

我能够使用 JUnit 的 Parameterized Runner 使用一些数据来测试 getDurationString(min,sec) 函数。我想使用相同的测试代码为 getDurationString(sec) 函数编写测试,但我不知道该怎么做。我看了一些stackoverflow question,但没有帮助(可能是因为我使用的是 JUnit4)。

知道如何使用 JUnit4 实现这一点吗?

更新:添加测试代码供参考

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

@RunWith(Parameterized.class)
class SolutionTest {

    @Parameterized.Parameters(name = "Test{index} -> {0} min {1} sec returns {2}")
    public static Iterable<Object[]> dataForTwoParameterMethod() {
        return Arrays.asList(new Object[][]{
                {-1, 10, "Invalid Value"}, //minutes < 0 returns "Invalid value"
                {1, -1, "Invalid Value"}, //Negative seconds value
                {61, 50, "01h 01m 50s"}, //valid value returns time in format "XXh YYm ZZs"
        });
    }

    int minute;
    int seconds;
    String durationString;

    public SolutionTest(int minute, int seconds, String durationString) {
        this.minute = minute;
        this.seconds = seconds;
        this.durationString = durationString;
    }

    @Test
    public void getDurationString() {
        assertEquals(durationString, Solution.getDurationString(minute, seconds));
    }

    @Test @Ignore("TODO: Not able to find a way to inject two different data into one JUnit Test")
    public void testGetDurationString() {
        //TODO: how to use two data in single data driven test
    }
}

【问题讨论】:

  • 能否请您展示您已经尝试过的内容?特别是,您对getDurationString(min,sec) 的工作测试看起来如何,您是如何尝试编写其他测试的?
  • @DirkHerrmann 添加了有问题的测试代码。

标签: java unit-testing junit tdd junit4


【解决方案1】:

如果我没听错的话,你有一组参数:

{-1, 10, "Invalid Value"}, //minutes < 0 returns "Invalid value"
{1, -1, "Invalid Value"}, //Negative seconds value
{61, 50, "01h 01m 50s"}, //valid value returns time in format "XXh YYm ZZs"

你用来测试两个参数的方法getDurationString。而且 - 这是我的理解 - 你还想提供第二组参数,也许

{-1, "Invalid Value"}, //seconds < 0 returns "Invalid value"minutes, "
{0, "00h 00m 00s"}, //boundary case, everything zero
{1, "00h 00m 01s"}, //last digit (seconds)
{10, "00h 00m 10s"}, //but last digit (seconds)
{59, "00h 00m 59s"}, //boundary case
{60, "00h 01m 00s"}, //boundary case, last digit (minutes)
...

测试单参数方法getDurationString。但是,要使用 JUnit4 执行此操作,您必须创建第二个测试类 - 您不能将两组不同的参数放入同一个类中。

您可以在一个带有参数化测试的类中拥有许多测试方法 - 但它们都将在同一组参数上运行,如@Parameterized.Parameters 中定义的那样。

请注意,这与在 JUnit5 中实现参数化测试的方式不同:在 JUnit5 中,对于每个单独的参数化测试方法,可以单独决定测试方法应接收哪些输入参数。这是通过使用测试数据来自的信息注释每个测试方法来完成的:https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

【讨论】:

  • 感谢@Dirik 的详细回答。我认为另一个测试类是唯一的方法。然后。我会接受的。或者我可以迁移到 JUnit5
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2011-05-31
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多