Reputation: 1684
Okay, so I have a coursework problem sheet I've just finished, I know it works fine but I wanted to write some jUnit tests for it just as some testing practice. It sparked some questions. I wrote a function called count
which takes in an int n
and sums up all values from 1 to n
and returns it. So, I wrote 4 test cases for this and assumed they should all go within the same test method as such:
@Test
public void testCount() {
assertEquals(0, SimmonsPS5.count(0)); // 1 obj in table with name => 0 permutations
assertEquals(1, SimmonsPS5.count(1)); // 2 obj in table with name => 1 permutation
assertEquals(3, SimmonsPS5.count(2)); // 2 obj in table with name => 3 permutations (3c2)
assertEquals(5050, SimmonsPS5.count(100)); // 100 obj in table with name => 100c2 permutations (100(100+1)/2)
}
But then if one fails the test (which will probably mean they all do) the error reporter will only say that one test failed, which is not correct.
Next, my main method in the class instantiates the class, runs a few functions then prints to stdout
. The main method does this by first taking in one parameter (file name) and then reading the file and doing some operations on it. So I want to test that it outputs the correct data for each file - should each of these test cases reside in the same test method, or separate ones? I opted for separate ones, but only down to the sole reason that I feel as though these are more prone to failure than the previous method. So if this is the correct way to test it, what should the test method be called? testMain1
, testMain2
etc? What is the 'accepted' nomenclature?
My main
test case:
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
@Test
public void testMain4in() {
SimmonsPS5.main(new String[]{"4.in"});
assertEquals(47485, Integer.parseInt(outContent.toString().trim()));
}
@Test
public void testMain5in() {
SimmonsPS5.main(new String[]{"5.in"});
assertEquals(1667, Integer.parseInt(outContent.toString().trim()));
}
@Test
public void testMain6in() {
SimmonsPS5.main(new String[]{"6.in"});
assertEquals(45871, Integer.parseInt(outContent.toString().trim()));
}
@Test
public void testMain7in() {
SimmonsPS5.main(new String[]{"7.in"});
assertEquals(324, Integer.parseInt(outContent.toString().trim()));
}
@Test
public void testMain8in() {
SimmonsPS5.main(new String[]{"8.in"});
assertEquals(1057527, Integer.parseInt(outContent.toString().trim()));
}
Note that the output to stdout
is a single integral number.
Upvotes: 1
Views: 235
Reputation: 40315
You want each test to test a specific feature. All of the tests performed should collectively be able to indicate if the feature is working correctly or isn't. So, e.g., in your first example:
@Test
public void testCount() {
assertEquals(0, SimmonsPS5.count(0)); // 1 obj in table with name => 0 permutations
assertEquals(1, SimmonsPS5.count(1)); // 2 obj in table with name => 1 permutation
assertEquals(3, SimmonsPS5.count(2)); // 2 obj in table with name => 3 permutations (3c2)
assertEquals(5050, SimmonsPS5.count(100)); // 100 obj in table with name => 100c2 permutations (100(100+1)/2)
}
This would be appropriate. You are testing to see if count()
is behaving as designed. If any of those fail, then count()
is not behaving as designed.
It does not matter how many of those tests fail -- the action you take if all of them fail will be the same as the action you take if one of them fails: Debug count()
.
Your second case:
The main method does this by first taking in one parameter (file name) and then reading the file and doing some operations on it. So I want to test that it outputs the correct data for each file - should each of these test cases reside in the same test method, or separate ones?
Whether or not you combine these into a single test depends on, conceptually, what you are testing. Are you testing to see if main()
works? Or are you testing to see if it works on a single, specific file? The former would be a more conceptually correct test, as you don't actually care that it works on your specific files, you really care that it works as designed for any files, and you come up with a set of test files that you believe accurately represents the types of files the program will operate on.
Upvotes: 2