【问题标题】:Junit 5 extension - how to access testDescriptorJunit 5 扩展 - 如何访问 testDescriptor
【发布时间】:2021-12-08 16:01:55
【问题描述】:

我正在使用@ParameterizedTest 为测试编写扩展。我的测试看起来像:

@ParameterizedTest
@ValueSource(strings={"string1", "string2"})
public void test1(String name){
    System.out.println(name);
}

在扩展中,如何让 TestDeciptor 找出当前处于活动状态的调用?

public class MyExtension implements BeforeEachCallBack{
   
 @Override
 public void beforeEach(ExtensionContext extensionContext)   
     //  What to do here? extensionContext.?
 }
}

【问题讨论】:

    标签: java junit5 junit5-extension-model


    【解决方案1】:

    正如你所看到的,它的包名 org.junit.platform.engine.TestDescriptor 不是 JUnit Jupiter API 的一部分,而 JUnit Jupiter API 以 org.junit.jupiter.api 作为其基础包。而且,org.junit.platform.engine 甚至没有被 API 导入。

    所以可悲的答案是你无法获得测试描述符,至少不能没有一些肮脏和不稳定的反射。但是,我很确定您想要访问的根本原因可以通过不同的方式得到缓解。那么你想达到什么目标呢?

    【讨论】:

    • 感谢您的回复。情况是:我需要实施 2 个测试。每个人在 Jira (Xray) 中都有一个唯一的 id - TestID1 和 TestID2。两者都可以使用@ParameterizedTest 在相同的测试方法中实现但是,我需要知道当前正在运行哪个,以便能够将它与正确的 Jira 票证配对。此信息在 TestDescriptor 对象中提供......有什么想法吗?
    • 你可以试试TestExecutionListener:junit.org/junit5/docs/current/user-guide/…。有了它,您就可以访问 TestIdentifier,从而获得唯一 ID。
    猜你喜欢
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2017-10-22
    相关资源
    最近更新 更多