如何在extensionContext中获取变量值

huangapple go评论120阅读模式
英文:

How to get variable value in extensionContext

问题

  1. @ExtendWith({TestReporter.class})
  2. private TestClass{
  3. String result;
  4. @Test
  5. void testA(){
  6. //some action here
  7. result = some result;
  8. }
  9. }
  1. public class TestReporter implements BeforeAllCallback, BeforeTestExecutionCallback, AfterAllCallback,
  2. TestWatcher {
  3. private static ExtentHtmlReporter htmlReporter;
  4. private static ExtentReports extent;
  5. private static ExtentTest test;
  6. @Override
  7. public void beforeAll(ExtensionContext context) throws Exception {
  8. //set up extent report
  9. }
  10. @Override
  11. public void testSuccessful(ExtensionContext context) {
  12. //not possible, but desired
  13. test.pass(context.getElement.get("result"), MediaEntityBuilder.createScreenCaptureFromPath("test"+count+".png").build());
  14. }
  15. }
英文:

How would I get the value of the result field into my TestReporter class?

  1. @ExtendWith({TestReporter.class})
  2. private TestClass{
  3. String result;
  4. @Test
  5. void testA(){
  6. //some action here
  7. result = some result;
  8. }
  9. }
  1. public class TestReporter implements BeforeAllCallback, BeforeTestExecutionCallback, AfterAllCallback,
  2. TestWatcher {
  3. private static ExtentHtmlReporter htmlReporter;
  4. private static ExtentReports extent;
  5. private static ExtentTest test;
  6. @Override
  7. public void beforeAll(ExtensionContext context) throws Exception {
  8. //set up extent report
  9. }
  10. @Override
  11. public void testSuccessful(ExtensionContext context) {
  12. //not possible, but desired
  13. test.pass(context.getElement.get("result"), MediaEntityBuilder.createScreenCaptureFromPath("test"+count+".png").build());
  14. }
  15. }

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

  1. @ExtendWith(TestReporter.class)
  2. public class TestClass {
  3. String result;
  4. @Test
  5. void testA() {
  6. result = "some result";
  7. }
  8. }
  9. class TestReporter implements AfterEachCallback {
  10. @Override
  11. public void afterEach(final ExtensionContext context) throws Exception {
  12. Object testInstance = context.getRequiredTestInstance();
  13. Field resultField = testInstance.getClass().getDeclaredField("result");
  14. String resultValue = (String) resultField.get(testInstance);
  15. System.out.println("Value of result: " + resultValue);
  16. }
  17. }

请注意,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

  1. @ExtendWith(TestReporter.class)
  2. public class TestClass {
  3. String result;
  4. @Test
  5. void testA() {
  6. result = "some result";
  7. }
  8. }
  9. class TestReporter implements AfterEachCallback {
  10. @Override
  11. public void afterEach(final ExtensionContext context) throws Exception {
  12. Object testInstance = context.getRequiredTestInstance();
  13. Field resultField = testInstance.getClass().getDeclaredField("result");
  14. String resultValue = (String) resultField.get(testInstance);
  15. System.out.println("Value of result: " + resultValue);
  16. }
  17. }

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.

huangapple
  • 本文由 发表于 2020年5月5日 01:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61597910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定