如何在extensionContext中获取变量值

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

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.

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:

确定