Search in sources :

Example 16 with Launcher

use of org.junit.platform.launcher.Launcher in project ant by apache.

the class JUnitLauncherTask method execute.

@Override
public void execute() throws BuildException {
    final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader executionCL = createClassLoaderForTestExecution();
        Thread.currentThread().setContextClassLoader(executionCL);
        final Launcher launcher = LauncherFactory.create();
        final List<TestRequest> requests = buildTestRequests();
        for (final TestRequest testRequest : requests) {
            try {
                final TestDefinition test = testRequest.getOwner();
                final LauncherDiscoveryRequest request = testRequest.getDiscoveryRequest().build();
                final List<TestExecutionListener> testExecutionListeners = new ArrayList<>();
                // a listener that we always put at the front of list of listeners
                // for this request.
                final Listener firstListener = new Listener();
                // we always enroll the summary generating listener, to the request, so that we
                // get to use some of the details of the summary for our further decision making
                testExecutionListeners.add(firstListener);
                testExecutionListeners.addAll(getListeners(testRequest, executionCL));
                final PrintStream originalSysOut = System.out;
                final PrintStream originalSysErr = System.err;
                try {
                    firstListener.switchedSysOutHandle = trySwitchSysOutErr(testRequest, StreamType.SYS_OUT);
                    firstListener.switchedSysErrHandle = trySwitchSysOutErr(testRequest, StreamType.SYS_ERR);
                    launcher.execute(request, testExecutionListeners.toArray(new TestExecutionListener[testExecutionListeners.size()]));
                } finally {
                    // switch back sysout/syserr to the original
                    try {
                        System.setOut(originalSysOut);
                    } catch (Exception e) {
                    // ignore
                    }
                    try {
                        System.setErr(originalSysErr);
                    } catch (Exception e) {
                    // ignore
                    }
                }
                handleTestExecutionCompletion(test, firstListener.getSummary());
            } finally {
                try {
                    testRequest.close();
                } catch (Exception e) {
                    // log and move on
                    log("Failed to cleanly close test request", e, Project.MSG_DEBUG);
                }
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(previousClassLoader);
    }
}
Also used : LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) PrintStream(java.io.PrintStream) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) ArrayList(java.util.ArrayList) AntClassLoader(org.apache.tools.ant.AntClassLoader) Launcher(org.junit.platform.launcher.Launcher) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException)

Example 17 with Launcher

use of org.junit.platform.launcher.Launcher in project junit5 by junit-team.

the class XmlReportsWritingListenerTests method executeTests.

private void executeTests(TestEngine engine, Path tempDirectory, Clock clock) {
    PrintWriter out = new PrintWriter(new StringWriter());
    XmlReportsWritingListener reportListener = new XmlReportsWritingListener(tempDirectory.toString(), out, clock);
    Launcher launcher = createLauncher(engine);
    launcher.registerTestExecutionListeners(reportListener);
    launcher.execute(request().selectors(selectUniqueId(UniqueId.forEngine(engine.getId()))).build(), new TestExecutionListener[0]);
}
Also used : StringWriter(java.io.StringWriter) Launcher(org.junit.platform.launcher.Launcher) LauncherFactoryForTestingPurposesOnly.createLauncher(org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly.createLauncher) PrintWriter(java.io.PrintWriter)

Example 18 with Launcher

use of org.junit.platform.launcher.Launcher in project junit5 by junit-team.

the class JUnitPlatformProviderTests method singleTestClassIsInvoked.

@Test
void singleTestClassIsInvoked() throws Exception {
    Launcher launcher = LauncherFactory.create();
    JUnitPlatformProvider provider = new JUnitPlatformProvider(providerParametersMock(), launcher);
    TestPlanSummaryListener executionListener = new TestPlanSummaryListener();
    launcher.registerTestExecutionListeners(executionListener);
    invokeProvider(provider, TestClass1.class);
    assertThat(executionListener.summaries).hasSize(1);
    TestExecutionSummary summary = executionListener.summaries.get(0);
    assertEquals(TestClass1.TESTS_FOUND, summary.getTestsFoundCount());
    assertEquals(TestClass1.TESTS_STARTED, summary.getTestsStartedCount());
    assertEquals(TestClass1.TESTS_SKIPPED, summary.getTestsSkippedCount());
    assertEquals(TestClass1.TESTS_SUCCEEDED, summary.getTestsSucceededCount());
    assertEquals(TestClass1.TESTS_ABORTED, summary.getTestsAbortedCount());
    assertEquals(TestClass1.TESTS_FAILED, summary.getTestsFailedCount());
}
Also used : TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) Launcher(org.junit.platform.launcher.Launcher) Test(org.junit.jupiter.api.Test)

Example 19 with Launcher

use of org.junit.platform.launcher.Launcher in project junit5 by junit-team.

the class JUnitPlatformProviderTests method testExecutionOfMatchingTestMethods.

void testExecutionOfMatchingTestMethods(Class<?> testClass, String pattern, String... expectedTestNames) throws Exception {
    TestListResolver testListResolver = new TestListResolver(pattern);
    ProviderParameters providerParameters = providerParametersMock(testListResolver, testClass);
    Launcher launcher = LauncherFactory.create();
    JUnitPlatformProvider provider = new JUnitPlatformProvider(providerParameters, launcher);
    TestPlanSummaryListener executionListener = new TestPlanSummaryListener();
    launcher.registerTestExecutionListeners(executionListener);
    invokeProvider(provider, null);
    assertEquals(1, executionListener.summaries.size());
    TestExecutionSummary summary = executionListener.summaries.get(0);
    int expectedCount = expectedTestNames.length;
    assertEquals(expectedCount, summary.getTestsFoundCount());
    assertEquals(expectedCount, summary.getTestsFailedCount());
    assertEquals(expectedCount, summary.getFailures().size());
    assertThat(failedTestDisplayNames(summary)).contains(expectedTestNames);
}
Also used : ProviderParameters(org.apache.maven.surefire.providerapi.ProviderParameters) TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) Launcher(org.junit.platform.launcher.Launcher) TestListResolver(org.apache.maven.surefire.testset.TestListResolver)

Example 20 with Launcher

use of org.junit.platform.launcher.Launcher in project junit5 by junit-team.

the class JUnitPlatformProviderTests method allDiscoveredTestsAreInvokedForNullArgument.

@Test
void allDiscoveredTestsAreInvokedForNullArgument() throws Exception {
    RunListener runListener = runListenerMock();
    ProviderParameters providerParameters = providerParametersMock(runListener, TestClass1.class, TestClass2.class);
    Launcher launcher = LauncherFactory.create();
    JUnitPlatformProvider provider = new JUnitPlatformProvider(providerParameters, launcher);
    TestPlanSummaryListener executionListener = new TestPlanSummaryListener();
    launcher.registerTestExecutionListeners(executionListener);
    invokeProvider(provider, null);
    InOrder inOrder = inOrder(runListener);
    inOrder.verify(runListener).testSetStarting(new SimpleReportEntry(JUnitPlatformProvider.class.getName(), TestClass1.class.getName()));
    inOrder.verify(runListener).testSetCompleted(new SimpleReportEntry(JUnitPlatformProvider.class.getName(), TestClass1.class.getName()));
    inOrder.verify(runListener).testSetStarting(new SimpleReportEntry(JUnitPlatformProvider.class.getName(), TestClass2.class.getName()));
    inOrder.verify(runListener).testSetCompleted(new SimpleReportEntry(JUnitPlatformProvider.class.getName(), TestClass2.class.getName()));
    assertThat(executionListener.summaries).hasSize(1);
    TestExecutionSummary summary = executionListener.summaries.get(0);
    assertEquals(TestClass1.TESTS_FOUND + TestClass2.TESTS_FOUND, summary.getTestsFoundCount());
    assertEquals(TestClass1.TESTS_STARTED + TestClass2.TESTS_STARTED, summary.getTestsStartedCount());
    assertEquals(TestClass1.TESTS_SKIPPED + TestClass2.TESTS_SKIPPED, summary.getTestsSkippedCount());
    assertEquals(TestClass1.TESTS_SUCCEEDED + TestClass2.TESTS_SUCCEEDED, summary.getTestsSucceededCount());
    assertEquals(TestClass1.TESTS_ABORTED + TestClass2.TESTS_ABORTED, summary.getTestsAbortedCount());
    assertEquals(TestClass1.TESTS_FAILED + TestClass2.TESTS_FAILED, summary.getTestsFailedCount());
}
Also used : ProviderParameters(org.apache.maven.surefire.providerapi.ProviderParameters) SimpleReportEntry(org.apache.maven.surefire.report.SimpleReportEntry) InOrder(org.mockito.InOrder) TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) Launcher(org.junit.platform.launcher.Launcher) RunListener(org.apache.maven.surefire.report.RunListener) Test(org.junit.jupiter.api.Test)

Aggregations

Launcher (org.junit.platform.launcher.Launcher)26 LauncherDiscoveryRequest (org.junit.platform.launcher.LauncherDiscoveryRequest)16 TestExecutionSummary (org.junit.platform.launcher.listeners.TestExecutionSummary)8 SummaryGeneratingListener (org.junit.platform.launcher.listeners.SummaryGeneratingListener)7 TestExecutionListener (org.junit.platform.launcher.TestExecutionListener)6 TestPlan (org.junit.platform.launcher.TestPlan)6 Test (org.junit.jupiter.api.Test)5 ProviderParameters (org.apache.maven.surefire.providerapi.ProviderParameters)3 TestIdentifier (org.junit.platform.launcher.TestIdentifier)3 PrintStream (java.io.PrintStream)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 RunListener (org.apache.maven.surefire.report.RunListener)2 SimpleReportEntry (org.apache.maven.surefire.report.SimpleReportEntry)2 TestListResolver (org.apache.maven.surefire.testset.TestListResolver)2 TestsToRun (org.apache.maven.surefire.util.TestsToRun)2 TestExecutionResult (org.junit.platform.engine.TestExecutionResult)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1