Search in sources :

Example 1 with PostDiscoveryFilter

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

the class DefaultLauncherTests method prunesTestDescriptorsAfterApplyingPostDiscoveryFilters.

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

        @Override
        public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
            super.discover(discoveryRequest, uniqueId);
            TestDescriptorStub engineDescriptor = new TestDescriptorStub(uniqueId, uniqueId.toString());
            TestDescriptorStub 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;
        }
    };
    DefaultLauncher launcher = createLauncher(engine);
    TestPlan testPlan = launcher.discover(request().filters((PostDiscoveryFilter) testDescriptor -> FilterResult.includedIf(testDescriptor.isContainer())).build());
    assertThat(testPlan.getRoots()).hasSize(1);
    TestIdentifier engineIdentifier = getOnlyElement(testPlan.getRoots());
    assertThat(testPlan.getChildren(engineIdentifier)).isEmpty();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) TestEngineSpy(org.junit.platform.engine.test.TestEngineSpy) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) TestPlan(org.junit.platform.launcher.TestPlan) EngineFilter.excludeEngines(org.junit.platform.launcher.EngineFilter.excludeEngines) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterResult(org.junit.platform.engine.FilterResult) TestEngine(org.junit.platform.engine.TestEngine) TestDescriptorStub(org.junit.platform.engine.test.TestDescriptorStub) TestExecutionResult.successful(org.junit.platform.engine.TestExecutionResult.successful) EngineExecutionListener(org.junit.platform.engine.EngineExecutionListener) 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) LauncherDiscoveryRequestBuilder.request(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request) EngineFilter.includeEngines(org.junit.platform.launcher.EngineFilter.includeEngines) CollectionUtils.getOnlyElement(org.junit.platform.commons.util.CollectionUtils.getOnlyElement) InOrder(org.mockito.InOrder) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) JUnitException(org.junit.platform.commons.JUnitException) TestEngineStub(org.junit.platform.engine.test.TestEngineStub) UniqueId(org.junit.platform.engine.UniqueId) Test(org.junit.jupiter.api.Test) PreconditionViolationException(org.junit.platform.commons.util.PreconditionViolationException) 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) DemoHierarchicalTestDescriptor(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestDescriptor) Mockito.inOrder(org.mockito.Mockito.inOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) ExecutionRequest(org.junit.platform.engine.ExecutionRequest) TestDescriptor(org.junit.platform.engine.TestDescriptor) DiscoverySelectors.selectPackage(org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) UniqueId(org.junit.platform.engine.UniqueId) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) TestPlan(org.junit.platform.launcher.TestPlan) TestIdentifier(org.junit.platform.launcher.TestIdentifier) TestDescriptorStub(org.junit.platform.engine.test.TestDescriptorStub) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TestEngineSpy(org.junit.platform.engine.test.TestEngineSpy) Test(org.junit.jupiter.api.Test)

Example 2 with PostDiscoveryFilter

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

the class DefaultLauncherTests method launcherAppliesPostDiscoveryFilters.

@Test
void launcherAppliesPostDiscoveryFilters() {
    DemoHierarchicalTestEngine engine = new DemoHierarchicalTestEngine("myEngine");
    DemoHierarchicalTestDescriptor test1 = engine.addTest("test1", noOp);
    engine.addTest("test2", noOp);
    DefaultLauncher launcher = createLauncher(engine);
    PostDiscoveryFilter includeWithUniqueIdContainsTest = new PostDiscoveryFilterStub(descriptor -> FilterResult.includedIf(descriptor.getUniqueId().toString().contains("test")), () -> "filter1");
    PostDiscoveryFilter includeWithUniqueIdContains1 = new PostDiscoveryFilterStub(descriptor -> FilterResult.includedIf(descriptor.getUniqueId().toString().contains("1")), () -> "filter2");
    TestPlan testPlan = // 
    launcher.discover(// 
    request().selectors(// 
    selectPackage("any")).filters(includeWithUniqueIdContainsTest, // 
    includeWithUniqueIdContains1).build());
    assertThat(testPlan.getChildren(UniqueId.forEngine("myEngine").toString())).hasSize(1);
    assertThat(testPlan.getTestIdentifier(test1.getUniqueId().toString())).isNotNull();
}
Also used : TestPlan(org.junit.platform.launcher.TestPlan) DemoHierarchicalTestDescriptor(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestDescriptor) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) PostDiscoveryFilterStub(org.junit.platform.launcher.PostDiscoveryFilterStub) Test(org.junit.jupiter.api.Test)

Example 3 with PostDiscoveryFilter

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

the class DiscoveryRequestCreatorTests method convertsTagOptions.

@Test
void convertsTagOptions() {
    options.setScanClasspath(true);
    options.setIncludedTagExpressions(asList("fast", "medium", "slow"));
    options.setExcludedTagExpressions(asList("slow"));
    LauncherDiscoveryRequest request = convert();
    List<PostDiscoveryFilter> postDiscoveryFilters = request.getPostDiscoveryFilters();
    assertThat(postDiscoveryFilters).hasSize(2);
    assertThat(postDiscoveryFilters.get(0).toString()).contains("TagFilter");
    assertThat(postDiscoveryFilters.get(1).toString()).contains("TagFilter");
}
Also used : LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) Test(org.junit.jupiter.api.Test)

Example 4 with PostDiscoveryFilter

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

the class VintageLauncherIntegrationTests method executesAllTestsForNotFilterableChildRunnerOfSuite.

@TrackLogRecords
@Test
void executesAllTestsForNotFilterableChildRunnerOfSuite(LogRecordListener logRecordListener) {
    Class<?> suiteClass = JUnit4SuiteOfSuiteWithFilterableChildRunner.class;
    Class<?> testClass = JUnit4TestCaseWithNotFilterableRunner.class;
    LauncherDiscoveryRequestBuilder request = // 
    request().selectors(// 
    selectClass(suiteClass)).filters((PostDiscoveryFilter) descriptor -> includedIf(descriptor.getDisplayName().contains("#1")));
    TestPlan testPlan = discover(request);
    logRecordListener.clear();
    assertThat(testPlan.getDescendants(getOnlyElement(testPlan.getRoots()))).hasSize(4);
    Map<TestIdentifier, TestExecutionResult> results = execute(request);
    // 
    assertThat(results.keySet().stream().map(TestIdentifier::getDisplayName)).containsExactlyInAnyOrder("JUnit Vintage", suiteClass.getName(), testClass.getName(), "Test #0", "Test #1");
    // 
    assertThat(logRecordListener.stream(RunnerTestDescriptor.class, Level.WARNING).map(LogRecord::getMessage)).containsExactly(// 
    "Runner " + Suite.class.getName() + " (used on class " + suiteClass.getName() + ")" + " was not able to satisfy all filter requests.");
}
Also used : JUnit4TestCaseWithNotFilterableRunner(org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithNotFilterableRunner) ParameterizedTestCase(org.junit.vintage.engine.samples.junit4.ParameterizedTestCase) Launcher(org.junit.platform.launcher.Launcher) TestPlan(org.junit.platform.launcher.TestPlan) JUnit4TestCaseWithNotFilterableRunner(org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithNotFilterableRunner) TagFilter.excludeTags(org.junit.platform.launcher.TagFilter.excludeTags) Categories(org.junit.vintage.engine.samples.junit4.Categories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DiscoverySelectors.selectClass(org.junit.platform.engine.discovery.DiscoverySelectors.selectClass) FilterResult.includedIf(org.junit.platform.engine.FilterResult.includedIf) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Level(java.util.logging.Level) LinkedHashMap(java.util.LinkedHashMap) RunnerTestDescriptor(org.junit.vintage.engine.descriptor.RunnerTestDescriptor) ENGINE_ID(org.junit.vintage.engine.descriptor.VintageTestDescriptor.ENGINE_ID) Map(java.util.Map) TrackLogRecords(org.junit.jupiter.engine.TrackLogRecords) LauncherFactory(org.junit.platform.launcher.core.LauncherFactory) NotFilterableRunner(org.junit.vintage.engine.samples.junit4.NotFilterableRunner) JUnit4SuiteOfSuiteWithFilterableChildRunner(org.junit.vintage.engine.samples.junit4.JUnit4SuiteOfSuiteWithFilterableChildRunner) LauncherDiscoveryRequestBuilder.request(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request) EngineFilter.includeEngines(org.junit.platform.launcher.EngineFilter.includeEngines) CollectionUtils.getOnlyElement(org.junit.platform.commons.util.CollectionUtils.getOnlyElement) PlainJUnit4TestCaseWithTwoTestMethods(org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithTwoTestMethods) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) EnclosedJUnit4TestCase(org.junit.vintage.engine.samples.junit4.EnclosedJUnit4TestCase) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) LogRecord(java.util.logging.LogRecord) JUnit4SuiteWithTwoTestCases(org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithTwoTestCases) Test(org.junit.jupiter.api.Test) LogRecordListener(org.junit.platform.commons.logging.LogRecordListener) TestIdentifier(org.junit.platform.launcher.TestIdentifier) FilterResult.excluded(org.junit.platform.engine.FilterResult.excluded) LauncherDiscoveryRequestBuilder(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder) Suite(org.junit.runners.Suite) PlainJUnit4TestCaseWithFiveTestMethods(org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods) TagFilter.includeTags(org.junit.platform.launcher.TagFilter.includeTags) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) RunnerTestDescriptor(org.junit.vintage.engine.descriptor.RunnerTestDescriptor) LauncherDiscoveryRequestBuilder(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder) TestPlan(org.junit.platform.launcher.TestPlan) LogRecord(java.util.logging.LogRecord) JUnit4SuiteOfSuiteWithFilterableChildRunner(org.junit.vintage.engine.samples.junit4.JUnit4SuiteOfSuiteWithFilterableChildRunner) TestIdentifier(org.junit.platform.launcher.TestIdentifier) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Test(org.junit.jupiter.api.Test) TrackLogRecords(org.junit.jupiter.engine.TrackLogRecords)

Example 5 with PostDiscoveryFilter

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

the class VintageLauncherIntegrationTests method filtersOutAllDescendantsOfParameterizedTestCase.

@Test
void filtersOutAllDescendantsOfParameterizedTestCase() {
    Class<?> testClass = ParameterizedTestCase.class;
    LauncherDiscoveryRequestBuilder request = // 
    request().selectors(// 
    selectClass(testClass)).filters((PostDiscoveryFilter) descriptor -> excluded("excluded"));
    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 : ParameterizedTestCase(org.junit.vintage.engine.samples.junit4.ParameterizedTestCase) Launcher(org.junit.platform.launcher.Launcher) TestPlan(org.junit.platform.launcher.TestPlan) JUnit4TestCaseWithNotFilterableRunner(org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithNotFilterableRunner) TagFilter.excludeTags(org.junit.platform.launcher.TagFilter.excludeTags) Categories(org.junit.vintage.engine.samples.junit4.Categories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DiscoverySelectors.selectClass(org.junit.platform.engine.discovery.DiscoverySelectors.selectClass) FilterResult.includedIf(org.junit.platform.engine.FilterResult.includedIf) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Level(java.util.logging.Level) LinkedHashMap(java.util.LinkedHashMap) RunnerTestDescriptor(org.junit.vintage.engine.descriptor.RunnerTestDescriptor) ENGINE_ID(org.junit.vintage.engine.descriptor.VintageTestDescriptor.ENGINE_ID) Map(java.util.Map) TrackLogRecords(org.junit.jupiter.engine.TrackLogRecords) LauncherFactory(org.junit.platform.launcher.core.LauncherFactory) NotFilterableRunner(org.junit.vintage.engine.samples.junit4.NotFilterableRunner) JUnit4SuiteOfSuiteWithFilterableChildRunner(org.junit.vintage.engine.samples.junit4.JUnit4SuiteOfSuiteWithFilterableChildRunner) LauncherDiscoveryRequestBuilder.request(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request) EngineFilter.includeEngines(org.junit.platform.launcher.EngineFilter.includeEngines) CollectionUtils.getOnlyElement(org.junit.platform.commons.util.CollectionUtils.getOnlyElement) PlainJUnit4TestCaseWithTwoTestMethods(org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithTwoTestMethods) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) EnclosedJUnit4TestCase(org.junit.vintage.engine.samples.junit4.EnclosedJUnit4TestCase) LauncherDiscoveryRequest(org.junit.platform.launcher.LauncherDiscoveryRequest) LogRecord(java.util.logging.LogRecord) JUnit4SuiteWithTwoTestCases(org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithTwoTestCases) Test(org.junit.jupiter.api.Test) LogRecordListener(org.junit.platform.commons.logging.LogRecordListener) TestIdentifier(org.junit.platform.launcher.TestIdentifier) FilterResult.excluded(org.junit.platform.engine.FilterResult.excluded) LauncherDiscoveryRequestBuilder(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder) Suite(org.junit.runners.Suite) PlainJUnit4TestCaseWithFiveTestMethods(org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods) TagFilter.includeTags(org.junit.platform.launcher.TagFilter.includeTags) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) LauncherDiscoveryRequestBuilder(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder) TestPlan(org.junit.platform.launcher.TestPlan) ParameterizedTestCase(org.junit.vintage.engine.samples.junit4.ParameterizedTestCase) TestIdentifier(org.junit.platform.launcher.TestIdentifier) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 PostDiscoveryFilter (org.junit.platform.launcher.PostDiscoveryFilter)6 TestPlan (org.junit.platform.launcher.TestPlan)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 CollectionUtils.getOnlyElement (org.junit.platform.commons.util.CollectionUtils.getOnlyElement)4 EngineFilter.includeEngines (org.junit.platform.launcher.EngineFilter.includeEngines)4 LauncherDiscoveryRequest (org.junit.platform.launcher.LauncherDiscoveryRequest)4 TestExecutionListener (org.junit.platform.launcher.TestExecutionListener)4 TestIdentifier (org.junit.platform.launcher.TestIdentifier)4 LauncherDiscoveryRequestBuilder.request (org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Level (java.util.logging.Level)3 LogRecord (java.util.logging.LogRecord)3 TrackLogRecords (org.junit.jupiter.engine.TrackLogRecords)3 LogRecordListener (org.junit.platform.commons.logging.LogRecordListener)3 FilterResult.excluded (org.junit.platform.engine.FilterResult.excluded)3 FilterResult.includedIf (org.junit.platform.engine.FilterResult.includedIf)3 TestExecutionResult (org.junit.platform.engine.TestExecutionResult)3 DiscoverySelectors.selectClass (org.junit.platform.engine.discovery.DiscoverySelectors.selectClass)3