package eu.yals.test.utils;

import eu.yals.test.TestApp;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

public class TestWatcherExtension implements TestWatcher, BeforeTestExecutionCallback, AfterEachCallback {

    private static final String BUILD_NAME =
            System.getProperty(TestApp.Properties.BUILD_NAME, TestApp.Defaults.BUILD_NAME);


    private String testName;

    private long testStartTime;
    private float testDurationInMillis;
    private boolean testSucceeded;

    /**
     * Very first stage of running test. We use it for getting test name and logging executing startup.
     * @param extensionContext JUnit's test {@link ExtensionContext}
     */
    @Override
    public void beforeTestExecution(ExtensionContext extensionContext) {
        testName = setTestNameFromContext(extensionContext);
        testStartTime = System.currentTimeMillis();
        System.out.printf("Starting.... build '%s'. Test: '%s%n", BUILD_NAME, testName);
    }

    /**
     * Marks test as succeeded.
     * @param context JUnit's test {@link ExtensionContext}
     */
    @Override
    public void testSuccessful(ExtensionContext context) {
        testDurationInMillis = System.currentTimeMillis() - testStartTime;
        testSucceeded = true;
    }

    /**
     * Marks test as failed.
     * @param context JUnit's test {@link ExtensionContext}
     * @param cause exception led to test failure
     */
    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
        testDurationInMillis = System.currentTimeMillis() - testStartTime;
        testSucceeded = false;
    }

    /**
     * Very last step of test execution.
     * @param extensionContext JUnit's test {@link ExtensionContext}
     */
    @Override
    public void afterEach(ExtensionContext extensionContext) {
        String testResult = testSucceeded ? "OK" : "FAIL";
        String timeTook = testDurationInMillis/1000 +" s";

        System.out.printf("Finished(%s) build '%s'. Test: '%s, Time elapsed: %s%n",
                testResult,
                BUILD_NAME,
                testName,
                timeTook);
    }

    private String setTestNameFromContext(final ExtensionContext context) {
        String testClassName = context.getRequiredTestClass().getSimpleName();
        String rawMethodName = context.getRequiredTestMethod().getName();
        String[] methodAndBrowserInfo = rawMethodName.split("\\[");
        if (methodAndBrowserInfo.length > 0) {
            String method = methodAndBrowserInfo[0];
            return String.format("%s.%s", testClassName, method);
        } else {
            return String.format("%s.%s", testClassName, rawMethodName);
        }
    }
}

@@ -55,66 +55,8 @@
 pp;%0A
-import org.junit.jupiter.api.extension.AfterEachCallback;%0A
 impo
@@ -318,27 +318,8 @@
 back
-, AfterEachCallback
  %7B%0A%0A
@@ -1366,16 +1366,37 @@
 = true;%0A
+        afterTest();%0A
     %7D%0A%0A 
@@ -1741,16 +1741,37 @@
  false;%0A
+        afterTest();%0A
     %7D%0A%0A 
@@ -1828,152 +1828,37 @@
    *
- @param extensionContext JUnit's test %7B@link ExtensionContext%7D%0A     */%0A    @Override%0A    public void afterEach(ExtensionContext extensionContext
+/%0A    private void afterTest(
 ) %7B%0A

Test Report problem solved (#278)