Search code examples
javajunitautomationjunit4suite

How to print log of tests (pass/failed) while they are running in suite?


I have suite which contains couple of tests. I want to print for each test if it pass or fail when the test finish to run. I don't want to add code for each test cause I have a lot of tests, I need to do it in the suite. This is an example of one suite that I have:

    @RunWith(ConcurrentSuite.class)
    @Concurrent(threads = 6)
    @Suite.SuiteClasses({
        test1.class,
        test2.class,
        test3.class,
        test4.class,
    })
    public class ExampleSuite {

    }

tnx!


Solution

  • if your test classes all inherit from an abstract base test class, you could add a Testwatcher to this one and then implement any reaction you want to a test fail or a test success :

    abstract class BaseTest {
    
    @Rule
    public TestWatcher watchman = new TestWatcher() {
    // define logger here
    
        @Override
        protected void failed(Throwable e, Description description) {
            logger.error(description, e);
        }
    
        @Override
        protected void succeeded(Description description) {
            logger.info(description);
        }
    };