I had a quite a few chats these days with people fro a more QAish background. Originally I intended just to look at selenium and webdriver. I showed some of the stuff, which I wrote in Java, to my QA colleagues. I used a demo webapp that implements a phonebook. My testcase just added a new contact and verified, that the user got a success message.
After they saw a bit of it, they asked me how they could separate out the testdata. I was a bit stunned, because, well it was all there – at least to me as a java developer. I had separated out everything into a method somewhat like this:
createNewContact("Felix", "Leipold");
So I could test for other inputs as well:
createNewContact("", "Invalid"); createNewContact("Invalid", ""); createNewContact("Holden", "Caulfield");
Obviously the expectation of QAs was to have an external file, e.g. a spreadsheet that feeds the data into the test code. I am very critical of this approach, but I felt like playing with junit 4 a bit. Junit 4 allows you to specify a runner four your test and indeed they have got one prepackaged that does parameterized tests. So I thought how about having a runner reads a spreadsheet and feeds the data into the test. As an example I use a very simplistic function:
double add(double a, double b) { return a + b; }
So to test this I created this table and saved it as mathTest.xls:
Then I went on to create a test case, like this:
@RunWith(SpreadsheetDriven.class) @Spreadsheet(name = "mathTest.xls") public class MathTest { double a; double b; double expectedSum; public MathTest(Double a, Double b, Double expectedSum) { this.a = a; this.b = b; this.expectedSum = expectedSum; } @Test public void testAdd(){ assertEquals(expectedSum, add(a,b)); } double add(double a, double b) { return a + b; } }
Essentially this thing parses the file specified in the annotation and passes it into the constructor. It’s not doing any fancy type conversions. All numerics are passed in as double, booleans as booleans, and everything else should be a string. Implementation and tests are to be found here and depend on apache poi being on the classpath for reading the excel file.
I am also quite proud of the reporting I get:
I am wondering, if all this is a good idea. But if it helps keeping people from using fit, it’s a start. What I realised by now is that actually table or data driven testing is much more appropriate for unit tests. Funnily enough that people actually do it the other way round. For unit tests I would actually recommend not to use a spreadsheet, but the original JUnit Parameterized
runner, which lets you define your data like this:
@Parameterized.Parameters public static List getParameters(){ return Arrays.asList(new Object[][] { {2,2,4}, {3,4,7}, {4,2,6}, {2,4,6}} ); }
Leave a Reply