我熟悉android中的JUnit测试。我的问题是如果我们使用计算器并且我们想测试加法运算。如果我们使用更多数量的测试用例(例如30)来测试加法运算。而不是重写测试用例 30 次,是否有任何通用的方法可以做到这一点,或者有没有任何方法可以将测试用例从 excel 表或 xml 文件中获取..?
请告诉我有没有更好的方法...
提前感谢
我熟悉android中的JUnit测试。我的问题是如果我们使用计算器并且我们想测试加法运算。如果我们使用更多数量的测试用例(例如30)来测试加法运算。而不是重写测试用例 30 次,是否有任何通用的方法可以做到这一点,或者有没有任何方法可以将测试用例从 excel 表或 xml 文件中获取..?
请告诉我有没有更好的方法...
提前感谢
您可以通过创建一个函数并在每个 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))