You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium TestNG中如何获取测试方法的优先级数值

在测试套件中获取测试方法的优先级数值

在Salesforce的测试框架里,没法直接通过常规方法拿到@Test注解里的priority值,但可以用反射机制来实现这个功能。

具体实现方案:

  • 先获取当前正在执行的测试方法名称
  • 通过反射遍历当前类的方法,找到对应方法后读取@Test注解的priority属性

完整示例代码:

@Test (priority = 1, groups = { "requiredSetUp" })
public void login(){
    System.debug('This test is assigned ' + getPriorityNumber());
}

private Integer getPriorityNumber() {
    // 获取当前测试方法的名称
    String currentMethod = Test.getCurrentTest().getCurrentMethodName();
    // 遍历当前类的所有方法
    for(Method method : this.getClass().getMethods()) {
        if(method.getName() == currentMethod) {
            // 获取方法上的@Test注解实例
            Test.TestAnnotation testAnnotation = (Test.TestAnnotation)method.getAnnotation(Test.TestAnnotation.class);
            if(testAnnotation != null) {
                // 返回优先级数值,未指定时默认是0
                return testAnnotation.priority();
            }
        }
    }
    return null;
}

几点说明:

  • 这个方案依赖Salesforce的反射API,确保运行环境支持反射操作
  • 如果测试方法没显式设置priority,注解会返回默认值0

内容的提问来源于stack exchange,提问作者saint

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:50:26