【问题标题】:Retrieve Junit test expected result检索 Junit 测试预期结果
【发布时间】:2015-12-09 05:11:58
【问题描述】:

我正在为一堆类编写 Junit 测试用例;他们每个人都有一些测试方法。我要测试的类如下所示。

class A{
    int getNth(int n);
    int getCount();
}

class B{
    int[] getAllNth(int n);
    int getMin();
}

我将每个class.method() 的预期结果存储在一个文件中。例如,在 CSV 中,

A; getNth(1):7;        getNth(2):3;          getCount():3
B; getAllNth(2):[7,3]; getAllNth(3):[7,3,4]; getMin():3

我的问题是如何在测试用例中轻松检索这些值。我希望将方法调用A.getNth(2)传递给一个可以构建字符串的类"A.getNth(2)"

如果我存储数据的格式不理想,也欢迎提出建议。

【问题讨论】:

    标签: java junit


    【解决方案1】:

    听起来您可能想使用Fitnesse

    【讨论】:

      【解决方案2】:

      不确定 JUnit,但您可以通过以下方式使用 TestNG 使用数据提供程序:

      @DataProvider
      public Object[][] dp() {
        return new Object[][] {
          new Object[] { 1, 7 },
          new Object[] { 2, 3 },
        };
      }
      
      @Test(dataProvider = "dp")
      public nthShouldMatch(int parameter, int expected) {
        Assert.assertEquals(getNth(parameter), expected);
      }
      

      显然,您应该以从电子表格中检索其值的方式实现 dp(),而不是像我刚才那样对它们进行硬编码,但您明白了。实现数据提供程序后,您只需更新电子表格,甚至无需重新编译代码。

      【讨论】:

        猜你喜欢
        • 2011-05-04
        • 2015-06-12
        • 2016-09-18
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多