android中的JUnit

android中的JUnit,android,unit-testing,junit,test-data,Android,Unit Testing,Junit,Test Data,我熟悉android中的JUnit测试。。 我的问题是,如果我们使用计算器,并且我们想要测试加法运算..如果我们使用更多的测试用例(例如30个),则测试加法运算。除了重写测试用例30次之外,有没有什么通用的方法可以做到这一点,或者有没有什么方法可以将测试用例从excel工作表或xml文件中提取出来 请让我知道有没有更好的方法 感谢advace通过创建函数并在每个JUnit测试中调用它,您可以减少重复代码。例如: public void test_add_zeroPlusZero() { As

我熟悉android中的JUnit测试。。 我的问题是,如果我们使用计算器,并且我们想要测试加法运算..如果我们使用更多的测试用例(例如30个),则测试加法运算。除了重写测试用例30次之外,有没有什么通用的方法可以做到这一点,或者有没有什么方法可以将测试用例从excel工作表或xml文件中提取出来

请让我知道有没有更好的方法


感谢advace

通过创建函数并在每个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.
}
至于读取大量的测试值,您难道不能从文件中读取多行整数(表示要添加的值和预期的结果,每个值用空格或某物分隔),然后通过上面显示的TestAddd函数(或等效函数)将它们放进去吗?在伪代码中,这可能看起来像:

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))