【问题标题】:JUnit in androidandroid中的JUnit
【发布时间】:2011-05-04 07:57:58
【问题描述】:

我熟悉android中的JUnit测试.. 我的问题是如果我们使用计算器并且我们想测试加法运算..如果我们使用更多数量的测试用例(例如 30)来测试加法运算。而不是重写测试用例 30 次,是否有任何通用的方法可以做到这一点,或者有没有任何方法可以将测试用例从 excel 表或 xml 文件中获取..?

请告诉我有没有更好的方法...

提前感谢

【问题讨论】:

    标签: android unit-testing junit test-data


    【解决方案1】:

    您可以通过创建一个函数并在每个 JUnit 测试中调用它来减少重复代码。例如:

    public void test_add_zeroPlusZero()
    {
      Assert.assertTrue("Failed on 0+0.", testAdd(new int[] {0,0}),0);
    }
    
    private boolean testAdd(int[] values, int expectedValue)
    {
      // Try to add.
      // If adding fails, return false.
      // If adding succeeds, return true.
    }
    

    至于读取大量的测试值,你不能从文件中读取多行整数(表示要添加的值和预期的结果,每个值用空格或其他东西分隔)然后放入通过上面显示的 testAdd 函数(或等效函数)?在伪代码中,这可能看起来像:

    public void test_add_from_file()
      File testValueFile = get file with test values()
      while ((nextLine = testValueFile.readLine) != END_OF_FILE)
        int[] values = parse values from nextLine
        int expectedValue = parse expected value from nextLine
        Assert.assertTrue("Couldn't add the following values: " + values, testAdd(values, expectedValue))
    

    【讨论】:

      猜你喜欢
      • 2015-01-28
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      相关资源
      最近更新 更多