【问题标题】:Adding own annotation to dynamic skip test in TestNG在TestNG中为动态跳过测试添加自己的注释
【发布时间】:2015-01-29 21:37:21
【问题描述】:

当某些环境变量的值不可接受时,我想提供一种优雅的机制来跳过选定的测试。我选择添加自己的注释@RunCondition 来定义特定测试允许的值。然后我为 TestNG 创建了自己的侦听器,当环境变量的值不在注释参数中定义的允许范围内时,它将测试标记为禁用。

我的代码如下:

public class ExampleTest {

    private int envVar;

    @BeforeClass
    public void setUp() {
        //set up of some environmental variables which depends on external source
        StaticContext.setVar(getValueFromOuterSpace());
    }

    @RunCondition(envVar=2)
    @Test
    public void testFoo(){

    }
}


public class SkipTestTransformer implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {
        RunCondition annotation = method.getAnnotation(RunCondition.class);
        int[] admissibleValues = annotation.envVar();
        for (int val : admissibleValues) {
            if (StaticContext.getVar() == val) {
                return; // if environmental variable matches one of admissible values then do not skip
            }
        }
        iTestAnnotation.setEnabled(false);
    }
}

public @interface RunCondition {
    int[] envVar();
}

我的代码运行良好,但有一个小问题是 transform 方法在 setUp 之前调用,即 @BeforeClass 函数。在所有测试初始化​​之后还有其他可能运行 Transformer 吗?我认为这样的解决方案优雅而清晰,我不希望任何丑陋的 if 子句达到我的目标......

我正在使用 Java 7 和 TestNG v5.11。

【问题讨论】:

    标签: java testng


    【解决方案1】:

    尝试实现 IMethodInterceptor(在 TestNG 开始调用测试方法之前将调用此类的一个实例。)而不是注解转换器。它将允许管理将要执行的测试列表。它还允许使用您的测试注释。限制是具有依赖关系的测试方法不会传递给拦截方法。

    【讨论】:

    • 这就是我想要的。谢谢!
    【解决方案2】:

    有一个由测试框架直接支持的更好的概念,称为假设。您不应禁用测试,而应跳过执行:

    • 在 JUnit 中,您可以使用 assumeThat(boolean) 系列方法
    • 在TestNG中你可以抛出SkipException

    在这种情况下该方法不会消失,它会被标记为已跳过。

    【讨论】:

    • 但是当我解析注释时我应该如何抛出SkipException?当我在每个测试中分别检查跳过的条件时,您的解决方案将完美运行。
    • 啊,从描述中看不清楚。所以你能再解释一下吗。您对它有什么期望,在示例中添加更多测试,...
    • 我将创建一个辅助方法assumeThat(boolean),它将在现在使用@RunCondition 的每个方法中调用。我会在assumeThat() 的参数中移动表达式。
    【解决方案3】:

    您可以在设置方法 (@BeforeMethod) 中检查自己的注释并抛出 SkipException 以跳过此测试。

    public class ExampleTest {
    
        private int envVar;
    
        @BeforeClass
        public void setUp() {
            //set up of some environmental variables which depends on external source
            StaticContext.setVar(2);
        }
    
        @BeforeMethod
        public void checkRunCondition(Method method) {
            RunCondition annotation = method.getAnnotation(RunCondition.class);
            if (annotation != null) {
                int[] admissibleValues = annotation.envVar();
                for (int val : admissibleValues) {
                    if (StaticContext.getVar() == val) {
                        // if environmental variable matches one of admissible values then do not skip
                        throw new SkipException("skip because of RunCondition");
                    }
                }
            }
        }
    
        @RunCondition(envVar = 2)
        @Test
        public void testFoo() {
    
        }
    
        @Retention(RetentionPolicy.RUNTIME)
        public @interface RunCondition {
    
            int[] envVar();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多