我熟悉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.
}

至于读取大量测试值,您不能只读取多个整数的整数(代表要添加的值和预期的结果,每个值以空间或某物分离的值),然后将它们通过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))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top