简体   繁体   English

如何以编程方式运行 JUnit 测试并指定类路径

[英]How to run a JUnit test programatically and specify a classpath

I am writing a custom tool that executes a set of test classes programmatically using JUnit 5.我正在编写一个自定义工具,它使用 JUnit 5 以编程方式执行一组测试类。

When executing these tests, is there a way to define the classpath for the execution?在执行这些测试时,有没有办法为执行定义类路径? (I have a set of jars that are being provided, as well as some compiled production and test code which I will need to add to the classpath). (我有一组正在提供的 jars,以及一些我需要添加到类路径中的已编译生产和测试代码)。

I have roughly the following code:我大致有以下代码:

LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                                                                  .selectors(selectPackage(basePackage))
                                                                  .filters(includeClassNamePatterns(classNamePatterns))
                                                                  .build();

SummaryGeneratingListener listener = new SummaryGeneratingListener();

Launcher launcher = LauncherFactory.create();
launcher.registerTestExecutionListeners(listener);

TestPlan testPlan = launcher.discover(request);

// launcher.execute(request);
launcher.execute(testPlan);

TestExecutionSummary summary = listener.getSummary();
summary.printTo(new PrintWriter(System.out));
summary.printFailuresTo(new PrintWriter(System.out));

What's the correct way to do this?这样做的正确方法是什么?

When building the request you can select classpath roots via org.junit.platform.engine.discovery.ClasspathRootSelector .构建请求时,您可以通过org.junit.platform.engine.discovery.ClasspathRootSelector select 类路径根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM