@SuppressWarnings("unused") public static void main(String[] args) { // Discover and filter tests LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder .request() .selectors(selectPackage("io.github.bonigarcia"), selectClass(DummyTest.class)) .filters(includeClassNamePatterns(".*Test")).build(); Launcher launcher = LauncherFactory.create(); TestPlan plan = launcher.discover(request); // Executing tests TestExecutionListener listener = new SummaryGeneratingListener(); launcher.registerTestExecutionListeners(listener); launcher.execute(request, listener); }
@Override public List<TestUnit> findTestUnits(Class<?> clazz) { TestPlan testPlan = launcher.discover(LauncherDiscoveryRequestBuilder .request() .selectors(DiscoverySelectors.selectClass(clazz)) .build()); return testPlan.getRoots() .stream() .map(testPlan::getDescendants) .flatMap(Set::stream) .filter(testIdentifier -> testIdentifier.getSource().isPresent()) .filter(testIdentifier -> testIdentifier.getSource().get() instanceof MethodSource) .filter(testIdentifier -> ((MethodSource) testIdentifier.getSource().get()).getClassName().equals(clazz.getName())) .map(testIdentifier -> new JUnit5TestUnit(testIdentifier)) .collect(toList()); }
@Override public void testPlanExecutionStarted(TestPlan testPlan) { executionReportBuilder = TestPlanExecutionReport.builder(testPlan); executionReportBuilder.addAllClassesToSkip(classesToSkip); classesToSkip.clear(); }
public static Builder builder(TestPlan testPlan) { return new AutoValue_TestPlanExecutionReport.Builder().setTestPlan(testPlan); }
final Builder setTestPlan(TestPlan testPlan) { this.testPlan = testPlan; return this; }
@Override public void testPlanExecutionFinished(TestPlan testPlan) { super.testPlanExecutionFinished(testPlan); long tests = testPlan.countTestIdentifiers(TestIdentifier::isTest); System.out.println("Test execution finished. Number of all tests: " + tests); }