@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()); }
public static void main(String[] args) throws Exception { try (SocketChannel channel = openSocket()) { TestExecutionListener listener = createListener(channel); Launcher launcher = LauncherFactory.create(); launcher.registerTestExecutionListeners(listener); ObjectInputStream inputStream = openInput(); Map<String, List<String>> config = (Map<String, List<String>>) inputStream.readObject(); Set<File> roots = config.get("classpathRoots").stream() .map(File::new) .collect(Collectors.toSet()); LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectClasspathRoots(roots)) .build(); launcher.execute(request); } }
@Test public void selectingByNamespace() { EngineDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectNamespace("sample.other-test")) .build(); UniqueId root = UniqueId.root("sample", "test"); List<UniqueId> expectedIds = Stream.of( root.append("namespace", "sample.other-test"), root.append("namespace", "sample.other-test").append("name", "my-other-works"), root.append("namespace", "sample.other-test").append("name", "my-other-fails"), root.append("namespace", "sample.other-test").append("name", "my-other-error") ).collect(Collectors.toList()); TestDescriptor descriptor = new ClojureTestEngine().discover(request, root); List<UniqueId> actualIds = descriptor.getAllDescendants().stream() .map(TestDescriptor::getUniqueId) .collect(Collectors.toList()); assertEquals(expectedIds, actualIds); }
@Test public void selectingByVar() { EngineDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectVar("sample.other-test", "my-other-works")) .build(); UniqueId root = UniqueId.root("sample", "test"); List<UniqueId> expectedIds = Stream.of( root.append("namespace", "sample.other-test"), root.append("namespace", "sample.other-test").append("name", "my-other-works") ).collect(Collectors.toList()); TestDescriptor descriptor = new ClojureTestEngine().discover(request, root); List<UniqueId> actualIds = descriptor.getAllDescendants().stream() .map(TestDescriptor::getUniqueId) .collect(Collectors.toList()); assertEquals(expectedIds, actualIds); }
@Test public void filteringByNamespace() { Set<File> roots = Arrays.stream(System.getProperty("classpath.roots").split(File.pathSeparator)) .map(File::new) .collect(Collectors.toSet()); EngineDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectClasspathRoots(roots)) .filters(includeNamespacePattern(".*other.*")) .build(); UniqueId root = UniqueId.root("sample", "test"); List<UniqueId> expectedIds = Stream.of( root.append("namespace", "sample.other-test"), root.append("namespace", "sample.other-test").append("name", "my-other-works"), root.append("namespace", "sample.other-test").append("name", "my-other-fails"), root.append("namespace", "sample.other-test").append("name", "my-other-error") ).collect(Collectors.toList()); TestDescriptor descriptor = new ClojureTestEngine().discover(request, root); List<UniqueId> actualIds = descriptor.getAllDescendants().stream() .map(TestDescriptor::getUniqueId) .collect(Collectors.toList()); assertEquals(expectedIds, actualIds); }
@Test public void getsTagsFromMetadata() { Set<File> roots = Arrays.stream(System.getProperty("classpath.roots").split(File.pathSeparator)) .map(File::new) .collect(Collectors.toSet()); EngineDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectClasspathRoots(roots)) .build(); UniqueId root = UniqueId.root("sample", "test"); Map<UniqueId, Set<TestTag>> expectedTags = new HashMap<>(); expectedTags.put(root.append("namespace", "sample.core-test"), tags("integration")); expectedTags.put(root.append("namespace", "sample.other-test"), tags()); expectedTags.put(root.append("namespace", "sample.core-test").append("name", "my-sample-works"), tags("integration")); expectedTags.put(root.append("namespace", "sample.core-test").append("name", "my-sample-fails"), tags()); expectedTags.put(root.append("namespace", "sample.other-test").append("name", "my-other-works"), tags("unit")); expectedTags.put(root.append("namespace", "sample.other-test").append("name", "my-other-fails"), tags()); expectedTags.put(root.append("namespace", "sample.other-test").append("name", "my-other-error"), tags("integration")); TestDescriptor descriptor = new ClojureTestEngine().discover(request, root); Map<UniqueId, Set<TestTag>> actualTags = descriptor.getAllDescendants().stream() .collect(Collectors.toMap(TestDescriptor::getUniqueId, TestDescriptor::getTags)); assertEquals(expectedTags, actualTags); }
private static int launch(ModuleReference moduleReference, Launcher launcher) { LauncherDiscoveryRequestBuilder builder = LauncherDiscoveryRequestBuilder.request(); builder.selectors(selectModule(moduleReference.descriptor().name())); long startTimeMillis = System.currentTimeMillis(); LauncherDiscoveryRequest launcherDiscoveryRequest = builder.build(); SummaryGeneratingListener summaryGeneratingListener = new SummaryGeneratingListener(); launcher.execute(launcherDiscoveryRequest, summaryGeneratingListener); long duration = System.currentTimeMillis() - startTimeMillis; TestExecutionSummary summary = summaryGeneratingListener.getSummary(); int failures = (int) summary.getTestsFailedCount(); if (failures == 0) { long succeeded = summary.getTestsSucceededCount(); String moduleName = moduleReference.descriptor().toNameAndVersion(); System.out.printf("[tester] Successfully tested %s: %d tests in %d ms%n", moduleName, succeeded, duration); } else { StringWriter stringWriter = new StringWriter(); summary.printTo(new PrintWriter(stringWriter)); summary.printFailuresTo(new PrintWriter(stringWriter)); System.out.println(stringWriter); } return failures; }
private void runClasses(Class<?>... classes) { final ClassSelector[] classSelectors = Stream.of(classes) .map(DiscoverySelectors::selectClass) .toArray(ClassSelector[]::new); final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(classSelectors) .build(); AllureJunit5AnnotationProcessor.setLifecycle(lifecycle); final Launcher launcher = LauncherFactory.create(); launcher.execute(request, new AllureJunit5(lifecycle)); }
private void runClasses(Class<?>... classes) { final ClassSelector[] classSelectors = Stream.of(classes) .map(DiscoverySelectors::selectClass) .toArray(ClassSelector[]::new); final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(classSelectors) .build(); final Launcher launcher = LauncherFactory.create(); launcher.execute(request, new AllureJunit5(lifecycle)); }
public static TestPlanExecutionReport runTests(Class<?> clazz) { Launcher launcher = LauncherFactory.create(); LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectClass(clazz)) .build(); ExecutionReportListener listener = new ExecutionReportListener().addClassToSkip(clazz); launcher.execute(request, listener); return listener.getReport(); }
public ExitStatus execute(SummaryGeneratingListener listener, DiscoverySelector selector, ApplicationConfiguration applicationConfiguration, FilesLocator filesLocator) { ApplicationConfigurationResolver.setConfiguration(applicationConfiguration); FilesLocatorResolver.setFilesLocator(filesLocator); LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selector) .build(); Launcher launcher = LauncherFactory.create(); launcher.registerTestExecutionListeners(listener); launcher.execute(request); return listener.getSummary().getFailures().isEmpty() ? ExitStatus.SUCCESS : ExitStatus.FAILURE; }
@Test public void selectingByClasspathDir() { Set<File> roots = Arrays.stream(System.getProperty("classpath.roots").split(File.pathSeparator)) .map(File::new) .collect(Collectors.toSet()); EngineDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(selectClasspathRoots(roots)) .build(); UniqueId root = UniqueId.root("sample", "test"); List<UniqueId> expectedIds = Stream.of( root.append("namespace", "sample.core-test"), root.append("namespace", "sample.other-test"), root.append("namespace", "sample.core-test").append("name", "my-sample-works"), root.append("namespace", "sample.core-test").append("name", "my-sample-fails"), root.append("namespace", "sample.other-test").append("name", "my-other-works"), root.append("namespace", "sample.other-test").append("name", "my-other-fails"), root.append("namespace", "sample.other-test").append("name", "my-other-error") ).collect(Collectors.toList()); TestDescriptor descriptor = new ClojureTestEngine().discover(request, root); List<UniqueId> actualIds = descriptor.getAllDescendants().stream() .map(TestDescriptor::getUniqueId) .collect(Collectors.toList()); assertEquals(expectedIds, actualIds); }