【问题标题】:How do I disable a test from running in a subclass in TestNG?如何禁止测试在 TestNG 的子类中运行?
【发布时间】:2018-02-27 08:30:40
【问题描述】:

(更新:在我报告此事后,TestNG 团队confirmedthebug。)

通常,可以使用@Ignoreenabled=false 忽略一个类

这不适用于在其超类中定义测试方法的子类(并且子类在挂钩方法中定义其特定功能)。请参阅下面的ChildClassTest

请注意,@Ignore 特定于 JUnit,而 TestNG 使用 enabled

基类

import org.testng.annotations.Test;

public class ParentClassTest {
    @Test
    public void test1() {
        hook();
    }

    protected void hook() {};
}

儿童班

import org.testng.Reporter;
import org.testng.annotations.Ignore;

@Ignore
public class ChildClassTest extends ParentClassTest {
    @Override
    protected void hook() {
        Reporter.log("ChildClassTest#hook()");
    }
}

【问题讨论】:

  • 使用@Ignore注解
  • @Ignore 注释显然特定于 JUnit,在 TestNG 中不可用
  • Ignore a class in testng的可能重复
  • 在 TestNG 中忽略一个类很容易。只需使用启用=假。但是如果这个类是一个测试类的子类并且没有它自己的方法呢?这是我的问题
  • @user7 事实证明,这确定了一个真正的错误,并得到了 TestNg 团队的确认!

标签: java unit-testing testng


【解决方案1】:

出于好奇,进行了一些头脑风暴,并提出了以下使用 v6.14.2 测试的解决方法。我个人更喜欢第一个,更简洁、更优雅、更灵活、更易于维护和扩展。

上下文

import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

public class MyTest {
    @Test
    public void shouldRun() {
        assertTrue(true);
    }

    @Test
    public void shouldNotRun() {
        assertTrue(true);
    }

    @Test
    public void shouldNotRunEither() {
        assertTrue(true);
    }
}

1) 使用侦听器 - 创建 TestListenerAdapter 和注释以跳过具有特定名称的方法:灵活、清晰、易于重用和识别以删除。唯一的缺点是您必须注意拼写错误的方法名称。

注释

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SkipMethods {
    String[] value() default {};
}

TestListenerAdapter

import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.TestListenerAdapter;

public class TestSkippingListener extends TestListenerAdapter {
    @Override
    public void onTestStart(ITestResult result) {
        // get the skip annotation
        SkipMethods skipAnnotation = result.getMethod().getInstance().getClass().getAnnotation(SkipMethods.class);

        // if the annotation exists
        if (skipAnnotation != null) {
            for (String skippableMethod : skipAnnotation.value()) {

                // and defines the current method as skippable
                if (skippableMethod.equals(result.getMethod().getMethodName())) {

                    // skip it
                    throw new SkipException("Method [" + skippableMethod + "] marked for skipping");
                }
            }
        }
    }
}

测试子类

import org.testng.annotations.Listeners;

// use listener
@Listeners(TestSkippingListener.class)

// define what methods to skip
@SkipMethods({"shouldNotRun", "shouldNotRunEither"})
public class MyTestSkippingInheritedMethods extends MyTest {

}

结果


2) 覆盖超类中的方法并抛出SkipException:很清楚,没有错字的可能性,但不可重用,不易维护并引入无用代码:

import org.testng.SkipException;

public class MyTestSkippingInheritedMethods extends MyTest {

    @Override
    public void shouldNotRun() {
        throw new SkipException("Skipped");
    }

    @Override
    public void shouldNotRunEither() {
        throw new SkipException("Skipped");
    }
}

结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2015-02-18
    • 2020-12-17
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多