If you are using JUnit to execute, you can extend the TestWatcher class:
public class TestRules extends TestWatcher {
@Override
protected void failed(Throwable e, Description description) {
// This will be called whenever a test fails.
}
So in your test class you can si...