如何在 junit 5 扩展中获取重复计数
How to get repetition count in a junit 5 extension
我尝试编写自己的 JUnit 5 扩展,提供一些关于测试持续时间的简单信息。
我也想打印出重复信息,但如何在扩展中访问这些信息?
有什么简单的方法可以代替反射或将数字写入并解析为显示名称吗?
简单示例:
@ExtendWith(TimingExtension.class)
public class MyTestClass {
@RepeatedTest(value = 5, name = "{currentRepetition}/{totalRepetitions}")
public void myTest(TestInfo testInfo, RepetitionInfo repInfo) {
// do some work here...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if(context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null) {
System.out.println("This was test X of Y"); // how to get currentRepetition and totalRepetitions here?
}
}
}
遗憾的是,扩展中不支持参数注入。这只是一种方式。因此,为了在您的 TimingExtension
中获得 RepetitionInfo
,您必须设置它。
首先你需要使用@RegisterExtension
例如
public class MyTestClass {
@RegisterExtension
TimingExtension timingExt = new TimingExtension();
@RepeatedTest(value = 5, name = "{currentRepetition}/{totalRepetitions}")
public void myTest(TestInfo testInfo, RepetitionInfo repInfo) {
timingExt.setRepetitionInfo(repInfo);
// do some work here...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
private RepetitionInfo repInfo;
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if (context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null && repInfo != null) {
System.out.println(String.format("This was test %d of %d", repInfo.getCurrentRepetition(), repInfo.getTotalRepetitions()))
repInfo = null;
}
}
public void setRepetitionInfo(RepetitionInfo repInfo) {
this.repInfo = repInfo;
}
}
我尝试编写自己的 JUnit 5 扩展,提供一些关于测试持续时间的简单信息。 我也想打印出重复信息,但如何在扩展中访问这些信息? 有什么简单的方法可以代替反射或将数字写入并解析为显示名称吗?
简单示例:
@ExtendWith(TimingExtension.class)
public class MyTestClass {
@RepeatedTest(value = 5, name = "{currentRepetition}/{totalRepetitions}")
public void myTest(TestInfo testInfo, RepetitionInfo repInfo) {
// do some work here...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if(context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null) {
System.out.println("This was test X of Y"); // how to get currentRepetition and totalRepetitions here?
}
}
}
遗憾的是,扩展中不支持参数注入。这只是一种方式。因此,为了在您的 TimingExtension
中获得 RepetitionInfo
,您必须设置它。
首先你需要使用@RegisterExtension
例如
public class MyTestClass {
@RegisterExtension
TimingExtension timingExt = new TimingExtension();
@RepeatedTest(value = 5, name = "{currentRepetition}/{totalRepetitions}")
public void myTest(TestInfo testInfo, RepetitionInfo repInfo) {
timingExt.setRepetitionInfo(repInfo);
// do some work here...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
private RepetitionInfo repInfo;
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if (context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null && repInfo != null) {
System.out.println(String.format("This was test %d of %d", repInfo.getCurrentRepetition(), repInfo.getTotalRepetitions()))
repInfo = null;
}
}
public void setRepetitionInfo(RepetitionInfo repInfo) {
this.repInfo = repInfo;
}
}