微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Gradle 运行忽略测试在类级别进行参数化和忽略?

如何解决如何使用 Gradle 运行忽略测试在类级别进行参数化和忽略?

我在类级别有一组“参数化”测试,@Ignored 注意。看起来 gradle 根本没有检测到测试。如何从命令行运行这些测试?这是示例测试代码

gradle tests --tests some.bad.tests.ThisTestIsAnIgnoredTest

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;


@RunWith(Parameterized.class)
@Ignore
public class TestNumbers4IgnoredAtClassLevelParameterizedTest {
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"data1",},{"data2"},});
    }

    private String data;

    @Test
    public void ignoredInJunit4() {
        System.out.println(new Numbers().add(1,2));
        System.out.println(data);
    }
}

命令运行测试

 $CHS_HOME/bld/gradle/bin/gradle test --tests TestNumbers4IgnoredAtClassLevelParameterizedTest --info

结果

FAILURE: Build Failed with an exception.

* What went wrong:
Execution Failed for task ':test'.
> No tests found for given includes: [TestNumbers4IgnoredAtClassLevelParameterizedTest](--tests filter)

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

如果我将@Ignore 从类移动到方法级别,一切正常。

解决方法

我怀疑您可能不能 - 我不了解 JUnit 的内部结构,但 https://docs.gradle.org/current/userguide/java_testing.html 说:

为了操作,Test 任务类型只需要两个 信息:

  • 在哪里可以找到编译好的测试类(属性: Test.getTestClassesDirs())

  • 执行类路径,应该包含被测类 以及您正在使用的测试库(属性: Test.getClasspath())

因此,JUnit 很可能根本就没有编译 @Ignore 的方法,因为没有配置可以运行或不运行测试。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。