JUnit in android
I am familiar with JUnit testing in android.. My Question is if we are using calculator and we want to test addition operation..To test the addition operation if we are using more number of test cases(for example 30). instead of rewriting the test cases for 30 times, is there any generic way to do this or is there any way to ta开发者_JAVA百科ke the test cases form excel sheet or xml file..?
Please let me know is there any better way ...
Thanks in advace
You could reduce duplicate code by making a function and calling it in each JUnit test. E.g.:
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.
}
As for reading in huge numbers of test values, couldn't you just read in multiple rows of integers (representing the values to add and the expected result, each value separated by a space or something) from file and then put them through the testAdd function (or equivalent) shown above? In pseudocode, this might look like:
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))
精彩评论