如何在JUnit 5扩展中获取重复次数

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

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;

    }
}

huangapple
  • 本文由 发表于 2020年7月30日 15:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63168682.html
匿名

发表评论

匿名网友

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

确定