Launcher概述
Launcher是JUnit5的启动类,也是对启动进行扩展的主要入口,扩展通过实现自定义的TestEngine来自定义测试类的发现和执行逻辑以达到定制化测试的目的
Launcher启动示例代码
public static void main(String[] args) {
//设置搜索和过滤规则
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("zj"),
selectClass(*MyTest.class)
)
.filters(
includeClassNamePatterns(".*Tests")
)
.build();
Launcher launcher = LauncherFactory.create();
// Register a listener of your choice
//通过监听器来监听获取执行结果
TestExecutionListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
启动分为如下几步:
1. 构造LauncherDiscoveryRequest指定测试类的查找和过滤规则
2. 通过 LauncherFactory.create() 来获取Launcher默认实现
3. 通过添加TestExecutionListener来进行测试结果的监听
4. 执行launcher.execute(req)方法启动测试