2

I am trying to discover JUnit 5 tests with the help of LauncherDiscoveryRequest as described in the user guide. The code I am using looks as follows:

LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
    .filters(includeClassNamePatterns(".*"))
    .build();

TestPlan plan = LauncherFactory.create().discover(request);

for (TestIdentifier root : plan.getRoots()) {
    System.out.println("Root: " + root.toString());

    for (TestIdentifier test : plan.getChildren(root)) {
        System.out.println("Found test: " + test.toString());
    }
}

Does LauncherDiscoveryRequestBuilder only auto-discover tests on the classpath of the JVM running this code? I tried using the methods DiscoverySelectors.selectClasspathRoots and DiscoverySelectors.selectClasspathResource as selectors to provide root directories to be searched for test classes. However, I wasn't able to discover any tests. Do the parameters for selectClasspathRoots and selectClasspathResource have to point to the root directory containing the class files organized by package or do you provide the full path to each test class file?

I tried the following where /some/dir represents the root directory containing test class files:

File classesDir = new File("/some/dir");
LauncherDiscoveryRequestBuilder.request()
     .selectors(selectClasspathRoots(Collections.singleton(Paths.get(classesDir.toURI()))))
     .build();

I had a look at LauncherDiscoveryRequestBuilderTests.java but it wasn't very useful in figuring out why my example code doesn't work. How do I best diagnose the issue?

I am using the following dependencies:

  • org.junit.platform:junit-platform-engine:1.0.0-M3
  • org.junit.platform:junit-platform-launcher:1.0.0-M3
4

2 回答 2

2

是否LauncherDiscoveryRequestBuilder仅在运行此代码的 JVM 的类路径上自动发现测试?

简短回答:是的,Launcher只会在运行代码的 JVM 的类路径上自动发现测试。

通常,TestEngine实现负责解析选择器,例如ClasspathRootSelector您通过selectClasspathRoots(). Jupiter 和 Vintage 引擎将扫描提供的目录中的.class文件,然后尝试使用当前线程的上下文加载它们ClassLoader。如果后一步失败,该类将被静默忽略。

如果您想发现尚未在类路径上的测试,您需要创建一个新的ClassLoader并将其设为当前线程的 context ClassLoader。JUnit 平台ConsoleLauncher也这样做:

  1. 创建自定义ClassLoader
  2. Launcher使用替换的上下文调用ClassLoader
于 2016-12-30T09:46:13.283 回答
1

LauncherDiscoveryRequestBuilder 是否仅在运行此代码的 JVM 的类路径上自动发现测试?

不,它可以扫描 jvm 中的每个类。您必须提供至少一个选择器,但发现是通过类路径上的有组织的文件夹/包结构完成的。

添加过滤器以控制要扫描的类。

这是来自文档。

目前可以搜索类、方法、包中的所有类,甚至类路径中的所有测试。

所有发现选择器都是到达测试类的各种方式。

可能没有更多的上下文,但这是我可以根据文档和运行/调试下面的测试找出的全部内容。

ClasspathScannerTests

输出

Root: TestIdentifier [uniqueId = '[engine:junit-jupiter]', parentId = null, displayName = 'JUnit Jupiter', source = null, tags = [], test = false, container = true]
Found test: TestIdentifier [uniqueId = '[engine:junit-jupiter]/[class:discover.suite.Junit5TestCase]', parentId = '[engine:junit-jupiter]', displayName = 'Junit5TestCase', source = ClassSource [className = 'discover.suite.Junit5TestCase', filePosition = null], tags = [], test = false, container = true]

样本测试类

package discover.suite;

import org.junit.jupiter.api.Test;

public class Junit5TestCase {

    @Test
    public void junitMethod() {
    }

}

下面是几个使用不同选择器的例子。

    //By classpath root
    File classesDir = new File("C:\\Users\\me\\Junit5\\target\\test-classes");

    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectClasspathRoots(Collections.singleton(Paths.get(classesDir.toURI()))))
            .filters(includeClassNamePatterns(".*"))
            .build();

    //By package
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectPackage("discover.suite"))
            .filters(includeClassNamePatterns(".*"))
            .build();

    //By Class
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectClass(Junit5TestCase.class))
            .filters(includeClassNamePatterns(".*"))
            .build();

    //By Method
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectMethod(Junit5TestCase.class, "junitMethod"))
            .filters(includeClassNamePatterns(".*"))
            .build();
于 2016-12-30T03:15:12.597 回答