How would I get the value of the result field into my TestReporter class?
@ExtendWith({TestReporter.class})
private TestClass{
String result;
@Test
void testA(){
//some action here
result = some result;
}
}
public class TestReporter implements BeforeAllCallback, BeforeTestExecutionCallback, AfterAllCallback,
TestWatcher {
private static ExtentHtmlReporter htmlReporter;
private static ExtentReports extent;
private static ExtentTest test;
@Override
public void beforeAll(ExtensionContext context) throws Exception {
//set up extent report
}
@Override
public void testSuccessful(ExtensionContext context) {
//not possible, but desired
test.pass(context.getElement.get("result"), MediaEntityBuilder.createScreenCaptureFromPath("test"+count+".png").build());
}
}
I have been researching ways to do this but not sure if what Im looking for is even possible or how to implement
Use reflection in the extension that has to implement an appropriate callback, eg AfterEachCallback
. Grab the instance of the test class through context.getRequiredTestInstance()
.
@ExtendWith(TestReporter.class)
public class TestClass {
String result;
@Test
void testA() {
result = "some result";
}
}
class TestReporter implements AfterEachCallback {
@Override
public void afterEach(final ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Field resultField = testInstance.getClass().getDeclaredField("result");
String resultValue = (String) resultField.get(testInstance);
System.out.println("Value of result: " + resultValue);
}
}
Mind that an AfterAllContext
does NOT have access to the test instance because there is one instance per test method. Using TestWatcher
instead of AfterEachCallback
would also work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.