Search in sources :

Example 26 with TestPlan

use of org.junit.platform.launcher.TestPlan in project spring-boot by spring-projects.

the class ModifiedClassPathExtension method runTest.

private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable {
    Class<?> testClass = classLoader.loadClass(testClassName);
    Method testMethod = findMethod(testClass, testMethodName);
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request().selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
    Launcher launcher = LauncherFactory.create();
    TestPlan testPlan = launcher.discover(request);
    SummaryGeneratingListener listener = new SummaryGeneratingListener();
    launcher.registerTestExecutionListeners(listener);
    launcher.execute(testPlan);
    TestExecutionSummary summary = listener.getSummary();
    if (!CollectionUtils.isEmpty(summary.getFailures())) {
        throw summary.getFailures().get(0).getException();
    }
}
Also used : LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) TestPlan(org.junit.platform.launcher.TestPlan) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) Launcher(org.junit.platform.launcher.Launcher) Method(java.lang.reflect.Method)

Example 27 with TestPlan

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

the class CustomPostDiscoveryFilter method execution.

@org.junit.jupiter.api.Test
@SuppressWarnings("unused")
void execution() {
    // @formatter:off
    // tag::execution[]
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request().selectors(selectPackage("com.example.mytests"), selectClass(MyTestClass.class)).filters(includeClassNamePatterns(".*Tests")).build();
    SummaryGeneratingListener listener = new SummaryGeneratingListener();
    try (LauncherSession session = LauncherFactory.openSession()) {
        Launcher launcher = session.getLauncher();
        // Register a listener of your choice
        launcher.registerTestExecutionListeners(listener);
        // Discover tests and build a test plan
        TestPlan testPlan = launcher.discover(request);
        // Execute test plan
        launcher.execute(testPlan);
        // Alternatively, execute the request directly
        launcher.execute(request);
    }
    TestExecutionSummary summary = listener.getSummary();
// Do something with the summary...
// end::execution[]
// @formatter:on
}
Also used : LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) TestPlan(org.junit.platform.launcher.TestPlan) TestExecutionSummary(org.junit.platform.launcher.listeners.TestExecutionSummary) Launcher(org.junit.platform.launcher.Launcher) LauncherSession(org.junit.platform.launcher.LauncherSession)

Example 28 with TestPlan

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

the class DefaultLauncherTests method prunesTestDescriptorsAfterApplyingPostDiscoveryFilters.

@Test
void prunesTestDescriptorsAfterApplyingPostDiscoveryFilters() {
    var engine = new TestEngineSpy() {

        @Override
        public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
            super.discover(discoveryRequest, uniqueId);
            var engineDescriptor = new TestDescriptorStub(uniqueId, uniqueId.toString());
            var containerDescriptor = new TestDescriptorStub(uniqueId.append("container", "a"), "container") {

                @Override
                public Type getType() {
                    return Type.CONTAINER;
                }
            };
            containerDescriptor.addChild(new TestDescriptorStub(containerDescriptor.getUniqueId().append("test", "b"), "test"));
            engineDescriptor.addChild(containerDescriptor);
            return engineDescriptor;
        }
    };
    var launcher = createLauncher(engine);
    var testPlan = launcher.discover(request().filters((PostDiscoveryFilter) testDescriptor -> FilterResult.includedIf(testDescriptor.isContainer())).build());
    assertThat(testPlan.getRoots()).hasSize(1);
    var engineIdentifier = getOnlyElement(testPlan.getRoots());
    assertThat(testPlan.getChildren(engineIdentifier)).isEmpty();
}
Also used : DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) TestPlan(org.junit.platform.launcher.TestPlan) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TestEngineStub(org.junit.platform.fakes.TestEngineStub) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LauncherDiscoveryListener(org.junit.platform.launcher.LauncherDiscoveryListener) TestEngineSpy(org.junit.platform.fakes.TestEngineSpy) LauncherDiscoveryRequestBuilder.request(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request) TestDescriptorStub(org.junit.platform.fakes.TestDescriptorStub) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) JUnitException(org.junit.platform.commons.JUnitException) LogRecord(java.util.logging.LogRecord) Test(org.junit.jupiter.api.Test) LogRecordListener(org.junit.platform.commons.logging.LogRecordListener) TestIdentifier(org.junit.platform.launcher.TestIdentifier) PostDiscoveryFilterStub(org.junit.platform.launcher.PostDiscoveryFilterStub) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) PreconditionViolationException(org.junit.platform.commons.PreconditionViolationException) Mockito.inOrder(org.mockito.Mockito.inOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestDescriptor(org.junit.platform.engine.TestDescriptor) Mockito.mock(org.mockito.Mockito.mock) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DEFAULT_DISCOVERY_LISTENER_CONFIGURATION_PROPERTY_NAME(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.DEFAULT_DISCOVERY_LISTENER_CONFIGURATION_PROPERTY_NAME) EngineDescriptor(org.junit.platform.engine.support.descriptor.EngineDescriptor) EngineFilter.excludeEngines(org.junit.platform.launcher.EngineFilter.excludeEngines) FilterResult(org.junit.platform.engine.FilterResult) TestEngine(org.junit.platform.engine.TestEngine) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Level(java.util.logging.Level) EngineDiscoveryResult(org.junit.platform.launcher.EngineDiscoveryResult) TestExecutionResult.successful(org.junit.platform.engine.TestExecutionResult.successful) ArgumentCaptor(org.mockito.ArgumentCaptor) LauncherFactoryForTestingPurposesOnly.createLauncher(org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly.createLauncher) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TrackLogRecords(org.junit.jupiter.api.fixtures.TrackLogRecords) ValueSource(org.junit.jupiter.params.provider.ValueSource) EngineFilter.includeEngines(org.junit.platform.launcher.EngineFilter.includeEngines) CollectionUtils.getOnlyElement(org.junit.platform.commons.util.CollectionUtils.getOnlyElement) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) UniqueId(org.junit.platform.engine.UniqueId) Mockito.verify(org.mockito.Mockito.verify) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) ExecutionRequest(org.junit.platform.engine.ExecutionRequest) DiscoverySelectors.selectPackage(org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) TestDescriptorStub(org.junit.platform.fakes.TestDescriptorStub) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TestEngineSpy(org.junit.platform.fakes.TestEngineSpy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with TestPlan

use of org.junit.platform.launcher.TestPlan in project intellij-community by JetBrains.

the class JUnit5EventsTest method containerFailure.

@Test
void containerFailure() throws Exception {
    ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(UniqueId.forEngine("engine"), TestClass.class);
    TestDescriptor testDescriptor = new TestFactoryTestDescriptor(UniqueId.forEngine("engine1"), TestClass.class, TestClass.class.getDeclaredMethod("brokenStream"));
    TestIdentifier identifier = TestIdentifier.from(testDescriptor);
    final TestPlan testPlan = TestPlan.from(Collections.singleton(classTestDescriptor));
    myExecutionListener.sendTree(testPlan, "");
    myExecutionListener.executionStarted(identifier);
    myExecutionListener.executionFinished(identifier, TestExecutionResult.failed(new IllegalStateException()));
    Assertions.assertEquals("##teamcity[enteredTheMatrix]\n" + "##teamcity[treeEnded]\n" + "##teamcity[testSuiteStarted id='|[engine:engine1|]' name='brokenStream()']\n" + "\n" + "##teamcity[testStarted name='Class Configuration' locationHint='java:suite://com.intellij.junit5.JUnit5EventsTest$TestClass.brokenStream']\n" + "\n" + "##teamcity[testFailed name='Class Configuration' id='Class Configuration' details='TRACE' error='true' message='']\n" + "\n" + "##teamcity[testFinished name='Class Configuration']\n" + "##teamcity[testSuiteFinished  id='|[engine:engine1|]' name='brokenStream()']\n", StringUtil.convertLineSeparators(myBuf.toString()));
}
Also used : TestFactoryTestDescriptor(org.junit.jupiter.engine.descriptor.TestFactoryTestDescriptor) TestPlan(org.junit.platform.launcher.TestPlan) TestIdentifier(org.junit.platform.launcher.TestIdentifier) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) MethodTestDescriptor(org.junit.jupiter.engine.descriptor.MethodTestDescriptor) TestFactoryTestDescriptor(org.junit.jupiter.engine.descriptor.TestFactoryTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) TestDescriptor(org.junit.platform.engine.TestDescriptor)

Example 30 with TestPlan

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

the class VintageLauncherIntegrationTests method removesCompleteClassIfItHasExcludedTag.

@Test
void removesCompleteClassIfItHasExcludedTag() {
    Class<?> testClass = PlainJUnit4TestCaseWithFiveTestMethods.class;
    LauncherDiscoveryRequestBuilder request = // 
    request().selectors(// 
    selectClass(testClass)).filters(excludeTags(Categories.Plain.class.getName()));
    TestPlan testPlan = discover(request);
    assertThat(testPlan.getDescendants(getOnlyElement(testPlan.getRoots()))).isEmpty();
    Map<TestIdentifier, TestExecutionResult> results = execute(request);
    // 
    assertThat(results.keySet().stream().map(TestIdentifier::getDisplayName)).containsExactly("JUnit Vintage");
}
Also used : LauncherDiscoveryRequestBuilder(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder) Categories(org.junit.vintage.engine.samples.junit4.Categories) TestPlan(org.junit.platform.launcher.TestPlan) TestIdentifier(org.junit.platform.launcher.TestIdentifier) PlainJUnit4TestCaseWithFiveTestMethods(org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Test(org.junit.jupiter.api.Test)

Aggregations

TestPlan (org.junit.platform.launcher.TestPlan)47 Test (org.junit.jupiter.api.Test)38 TestIdentifier (org.junit.platform.launcher.TestIdentifier)24 TestExecutionResult (org.junit.platform.engine.TestExecutionResult)16 EngineDescriptor (org.junit.platform.engine.support.descriptor.EngineDescriptor)16 LauncherDiscoveryRequestBuilder (org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder)11 SimpleReportEntry (org.apache.maven.surefire.report.SimpleReportEntry)10 Categories (org.junit.vintage.engine.samples.junit4.Categories)10 TestDescriptorStub (org.junit.platform.engine.test.TestDescriptorStub)9 UniqueId (org.junit.platform.engine.UniqueId)8 Launcher (org.junit.platform.launcher.Launcher)8 LauncherDiscoveryRequest (org.junit.platform.launcher.LauncherDiscoveryRequest)8 ReportEntry (org.apache.maven.surefire.report.ReportEntry)7 TestDescriptor (org.junit.platform.engine.TestDescriptor)6 EnclosedJUnit4TestCase (org.junit.vintage.engine.samples.junit4.EnclosedJUnit4TestCase)6 PlainJUnit4TestCaseWithFiveTestMethods (org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)4 TestEngine (org.junit.platform.engine.TestEngine)4 TestExecutionListener (org.junit.platform.launcher.TestExecutionListener)4