Reputation: 337
Consider a JUnit method within a JUnit class. It has some tests and some logger.info/debug statements or whatever. Maybe you don't log inside Junit methods, but I do and it saves me lots of hassles for quickly knowing why some tests failed.
I would like to 'erase' those log statements if the method reaches the end, possibly with SLF4J
A simple way to implement that would be to
logger.tick();
Tells logger to flag further log statements.
logger.tack();
Erase all the log statements up to the tick flag. Problem is that you cannot erase stuff on the Eclipse Console ;).
This would make my life much happier now that i'm using Continuous JUnit Testing.
Upvotes: 2
Views: 760
Reputation: 30416
I won't feel self conscious about logging inside unit tests but I imagine most people do alright with the logging facility built into the Unit test framework already. However if you wanted to implement this, you need to capture logs in a variable (some might suggest a StringBuilder
) and then dump them at the end of the test based on some dead man's switch (so if the switch isn't hit the last act of the unit test is to dump the held back log).
An example might look like this:
public abstract class LoggedTest extends TestCase {
protected boolean displayLog;
protected StringBuilder log;
public void tick() {
displayLog = true;
}
public void tack() {
displayLog = false;
}
public void log(String msg) {
log.append(message);
}
protected void setUp() {
displayLog = false;
log = new StringBuilder();
}
protected void tearDown() {
if(displayLog) {
System.err.println(log);
}
}
}
Upvotes: 2