英文:
How to get variable value in extensionContext
问题
@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());
}
}
英文:
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
答案1
得分: 5
TLDR;
在必须实现适当回调的扩展中使用反射,例如 AfterEachCallback
。通过 context.getRequiredTestInstance()
获取测试类的实例。
Long Version
@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);
}
}
请注意,AfterAllContext
无法访问测试实例,因为每个测试方法都有一个实例。使用 TestWatcher
而不是 AfterEachCallback
也可以工作。
英文:
TLDR;
Use reflection in the extension that has to implement an appropriate callback, e.g. AfterEachCallback
. Grab the instance of the test class through context.getRequiredTestInstance()
.
Long Version
@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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论