英文:
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) {
// 在这里执行一些操作...
}
}
public class TimingExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
if (context.getRequiredTestMethod().getDeclaredAnnotation(RepeatedTest.class) != null) {
System.out.println("这是第 X 次测试,总共 Y 次"); // 如何在这里获取currentRepetition和totalRepetitions?
}
}
}
英文:
I try to write my own JUnit 5 extension, providing some simple information about test duration.
I also want to print out the repetition information but how can I access these informations in the extension?
Are there any simple ways instead of reflection or writing and parsing the numbers to the display name?
simple example:
@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?
}
}
}
答案1
得分: 0
很抱歉,扩展中不支持参数注入。这只支持单向。因此,为了在您的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);
// 在这里进行一些操作...
}
}
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("这是第%d次重复的测试,共%d次", repInfo.getCurrentRepetition(), repInfo.getTotalRepetitions()));
repInfo = null;
}
}
public void setRepetitionInfo(RepetitionInfo repInfo) {
this.repInfo = repInfo;
}
}
英文:
Unfortunately there is no support for parameter injection in extensions. It's only one way. So in order to get RepetitionInfo
in your TimingExtension
you have to set it.
First you need to use @RegisterExtension
e.g.
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论