My problem, in short, is that I want to run some JUnit tests from a command line and I would like to be able to see:
- A list of all tests (whether they ran or not).
- A list of all tests which passed.
- A list of all tests which failed
The reason for the first bullet, is because I believe if one test calls System.exit, the others won't run, I still want to know about these tests, even if they didn't run.
Lets say I have this test class:
import junit.framework.TestCase;
public class ALoadOfTests extends TestCase {
public void testFoo() {
int a = 5;
int b = 10;
int result = a + b;
assertEquals(15, result);
}
public void testBar() {
fail();
}
public void testBaz() {
fail();
}
}
If I run this using the commands:
javac -cp ".;C:\Users\username\Desktop\Test\junit-4.11.jar;C:\Users\username\Desktop\Test\hamcrest-core-1.3.jar" ALoadOfTests.java
java -cp ".;C:\Users\username\Desktop\Test\junit-4.11.jar;C:\Users\username\Desktop\Test\hamcrest-core-1.3.jar" junit.textui.TestRunner ALoadOfTsts
The output is:
.F.F.
Time: 0.002
There were 2 failures:
1) testBar(ALoadOfTests)junit.framework.AssertionFailedError
at ALoadOfTests.testBar(ALoadOfTests.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
2) testBaz(ALoadOfTests)junit.framework.AssertionFailedError
at ALoadOfTests.testBaz(ALoadOfTests.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
FAILURES!!!
Tests run: 3, Failures: 2, Errors: 0
The problem here is that the output doesn't tell me the name of testFoo which exists and passed. Furthermore, if I change one of them to use System.exit, then we don't get much output at all, we just get:
.F.F.
Ideally, to be able to get all the information I need, if I could print out a list of test names first, before any tests are ran (and any chance of System.exit) then I can see which passed and which failed (or didn't run).
If I could get output that just looks similar to:
Tests:
testFoo
TestBar
TestBaz
Results:
TestFoo(passed)
TestBar(failed:reason)
Then I could safely assume:
TestFoo passed
TestBar failed
TestBaz never ran, probably because some test called system.exit.
Is this possible using a custom runner? If it is, any hints on where to start would be greatly appreciated.
Unfortunately I have no control over the code, it should be assumed that it may contain System.exit, possibly, in every method. I also have little control over the tests, so printing out the test names in the setup method isn't ideal.
Thanks,