בוקר טוב,
להלן CLASS GenericExampleExtension שאת יכולה לכתוב בתשתית. ולהגדיר מה לבצע בכל שלב של הטסט.
הוא ממש INTERFACE ש JUNIT מספק,
AfterAll, AFTER EACH וכו. המתודות מקבלות אוביקט CONTEXT – זהו אוביקט של JUNIT והוא מכיל מידע על הטסט שרץ .
בכל מתודה כתבי קוד מתאים לצרכים שלך וכשהטסט יהיה בשלב המתאים למתודה – הוא יבצע את המתודה.
וזאת בתנאי שב CLASS טסט תצהירי שאת משתמש ב EXTENTION הזה .
כך:
@ExtendWith({GenericExampleExtension .class})
שורה זו תופיעה מעל חתימת ה CLASS טסט.
בהצלחה רבה
מוזמנת לשאול אם משהו לא ברור
דוגמה ל EXTENTION.
/**
* A generic extension implementation.
* This class is meant to serve as an example to copy parts from it and use them for other extension
* implementations.
* Note!! You don’t have to implement all the listed methods for all the extensions. Use this class as example only,
* choose the method that fit your needs and implements it’s interface only in the extension
*
* Some considerations when implementing extensions:
* 1. You must register the extension at the class level in order for it to be invoked.
* 2. afterAll and beforeAll work per container, which means that if you run several tests from different test
* classes these methods will be invoked more than once.
* 3. You may use more than 1 extension. Junit 5 guarantees that all extension methods will be executed.
* 4. However! Junit 5 does not guarantee the order of execution. So keep the single responsibility and
* independence of extensions.
*
*
* Some useful links to refer to:
* 1. Understanding Junit 5 extension model, from Junit5 user guide
* 2. Test lifecycle callback order is listed here
* 3. An example of VeriSoft: @see co.verisoft.selenium.framework.extensions.junit.PageSourceSaverExtension
* 4. Junit extension package javadoc
*
*
* @author Nir Gallner @ http://www.VeriSoft.co
* @since 2.0.3.9
*
*/
public class GenericExampleExtension implements
AfterTestExecutionCallback,
AfterAllCallback,
AfterEachCallback,
BeforeAllCallback,
BeforeEachCallback,
BeforeTestExecutionCallback,
TestWatcher {
private static final Logger logger = new ExtendedLog(Helper.class);
/**
* Callback that is invoked once after all tests in the current
* container.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void afterAll(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In afterAll”);
}
/**
* Callback that is invoked after an individual test and any
* user-defined teardown methods for that test have been executed.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void afterEach(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In afterEach”);
}
/**
* Callback that is invoked immediately after an individual test has
* been executed but before any user-defined teardown methods have been
* executed for that test.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In afterTestExecution”);
}
/**
* Callback that is invoked once before all tests in the current
* container.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeAll(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In beforeAll”);
}
/**
* Callback that is invoked before an individual test and any
* user-defined setup methods for that test have been executed.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeEach(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In beforeEach”);
}
/**
* Callback that is invoked immediately before an individual test is
* executed but after any user-defined setup methods have been executed
* for that test.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
logger.info(“GeneralExampleExtension- In beforeTestExecution”);
}
/**
* Invoked after a disabled test has been skipped.
*
*
The default implementation does nothing. Concrete implementations can
* override this method as appropriate.
*
* @param context the current extension context; never {@code null}
* @param reason the reason the test is disabled; never {@code null} but
* potentially empty
*/
@Override
public void testDisabled(ExtensionContext context, Optional<String> reason) {
String s = “GeneralExampleExtension- Test ” + context.getDisplayName() + ” was disabled”;
if (reason.isPresent())
s += “, reason: ” + reason.toString();
logger.info(s);
}
/**
* Invoked after a test has completed successfully.
*
*
The default implementation does nothing. Concrete implementations can
* override this method as appropriate.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void testSuccessful(ExtensionContext context) {
String s = “GeneralExampleExtension- Test ” + context.getDisplayName() + ” passed”;
logger.info(s);
}
/**
* Invoked after a test has been aborted.
*
*
The default implementation does nothing. Concrete implementations can
* override this method as appropriate.
*
* @param context the current extension context; never {@code null}
* @param cause the throwable responsible for the test being aborted; may be {@code null}
*/
@Override
public void testAborted(ExtensionContext context, Throwable cause) {
String s = “GeneralExampleExtension- Test ” + context.getDisplayName() + ” aborted”;
s += “, cause: ” + cause.getMessage();
logger.info(s);
}
/**
* Invoked after a test has failed.
*
*
The default implementation does nothing. Concrete implementations can
* override this method as appropriate.
*
* @param context the current extension context; never {@code null}
* @param cause the throwable that caused test failure; may be {@code null}
*/
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
String s = “GeneralExampleExtension- Test ” + context.getDisplayName() + ” failed”;
s += “, cause: ” + cause.getMessage();
logger.info(s);
}
}