问题描述
我在类级别有一组“参数化”测试,@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
解决方法
我怀疑您可能不能 - 我不了解 JUnit 的内部结构,但 https://docs.gradle.org/current/userguide/java_testing.html 说:
为了操作,Test 任务类型只需要两个 信息:
-
在哪里可以找到编译好的测试类(属性: Test.getTestClassesDirs())
-
执行类路径,应该包含被测类 以及您正在使用的测试库(属性: Test.getClasspath())
因此,JUnit 很可能根本就没有编译 @Ignore 的方法,因为没有配置可以运行或不运行测试。