import org.junit.jupiter.api.extension.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StopWatch; public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback, BeforeAllCallback, AfterAllCallback { private static final Logger log = LoggerFactory.getLogger("result"); private StopWatch stopWatch; @Override public void beforeAll(ExtensionContext ctx) { stopWatch = new StopWatch("Execution time of " + ctx.getRequiredTestClass().getSimpleName()); } @Override public void beforeTestExecution(ExtensionContext ctx) { log.info("Start stopWatch"); stopWatch.start(ctx.getDisplayName()); } @Override public void afterTestExecution(ExtensionContext ctx) { stopWatch.stop(); log.info("stop stopWatch"); } @Override public void afterAll(ExtensionContext ctx) { log.info('\n' + stopWatch.prettyPrint() + '\n'); } }
Source: https://habr.com/ru/post/420891/
All Articles