问题
When a given JUnit4 test runs, I want it to generate a logfile of all of its TestResults. I do not want to invoke this via CI/ant/maven. I want this to run anytime the JUnit test is called from anywhere. If that is not possible, then I would like to write my own runner which problematically invokes my AllTestSuites class and logs all the results itself.
Here would be one of my test classes:
public class SimpleTestSuite extends TestCase{
@Test
public void simpleTestPass() {
assertTrue(true);
}
@Test
public void simpleTestFail() {
assertTrue(false);
}
@Test
public void simpleTestException() {
throw new RuntimeException();
}
}
I included it in a TestSuite, which contains all of my test Suites to run:
@RunWith(Suite.class)
@Suite.SuiteClasses({SimpleTestSuite.class, ExampleSuite.class,})
public final class AllTestSuites {}
I want to invoke AllTestSuites and have it generate a log file such as below. Remember, I prefer to capture what is on the JUnit4 framework's result bus, and not to reinvent/create a new test runner.
simpleTestPass - pass - 1/1 assertions passed
simpleTestFail - fail - 0/1 assertions passed
simpleTestException - exception - stacktrace as follows...
回答1:
Add logger in you Test Class and Base Test Class and define as TestWatchMan
as below:
Logger logger = LoggerFactory.getLogger(TestCase.class);
@Rule public MethodRule watchman = new TestWatchman() {
public void starting(FrameworkMethod method) {
logger.info("Test {} is running.", method.getName());
}
public void succeeded(FrameworkMethod method) {
logger.info("Test {} succesfully run.", method.getName());
}
public void failed(Throwable e, FrameworkMethod method) {
logger.error("Test {} failed with {} reason.",
method.getName(), e.getMessage());
}
};
回答2:
You can use a JUnit TestWatcher.
A TestWatcher defines following methods:
- succeeded()
- failed()
- starting()
- finished()
which you can implement to get notified of events and write them to a File.
回答3:
JUnit will output those results to the standard System.out. Perhaps you could redirect that output to a file.
@BeforeClass
void redirectOut(){
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int arg0) throws IOException {
// TODO Auto-generated method stub
}
}));
}
来源:https://stackoverflow.com/questions/13199559/how-to-log-test-pass-fail-results-of-junit4-to-a-file