Search in sources :

Example 16 with Filter

use of org.junit.runner.manipulation.Filter in project junit4 by junit-team.

the class SingleMethodTest method filteringAwayEverythingThrowsException.

@Test(expected = NoTestsRemainException.class)
public void filteringAwayEverythingThrowsException() throws NoTestsRemainException {
    Filterable runner = (Filterable) Request.aClass(OneTimeSetup.class).getRunner();
    runner.filter(new Filter() {

        @Override
        public boolean shouldRun(Description description) {
            return false;
        }

        @Override
        public String describe() {
            return null;
        }
    });
}
Also used : Description(org.junit.runner.Description) Filter(org.junit.runner.manipulation.Filter) Filterable(org.junit.runner.manipulation.Filterable) Test(org.junit.Test)

Example 17 with Filter

use of org.junit.runner.manipulation.Filter in project junit4 by junit-team.

the class CategoryFilterFactoryTest method shouldCreateFilter.

@Test
public void shouldCreateFilter() throws Exception {
    FilterFactoryParams params = new FilterFactoryParams(createSuiteDescription(testName.getMethodName()), CategoryFilterFactoryStub.class.getName());
    Filter filter = categoryFilterFactory.createFilter(params);
    assertThat(filter, instanceOf(DummyFilter.class));
}
Also used : Filter(org.junit.runner.manipulation.Filter) FilterFactoryParams(org.junit.runner.FilterFactoryParams) Test(org.junit.Test)

Example 18 with Filter

use of org.junit.runner.manipulation.Filter in project bazel by bazelbuild.

the class JUnit4Runner method applyFilters.

/**
   * Apply command-line and sharding filters, if appropriate.<p>
   *
   * Note that this is carefully written to avoid running into potential
   * problems with the way runners implement filtering. The JavaDoc for
   * {@link org.junit.runner.manipulation.Filterable} states that tests that
   * don't match the filter should be removed, which implies if you apply two
   * filters, you will always get an intersection of the two. Unfortunately, the
   * filtering implementation of {@link org.junit.runners.ParentRunner} does not
   * do this, and instead uses a "last applied filter wins" strategy.<p>
   *
   * We work around potential problems by ensuring that if we apply a second
   * filter, the filter is more restrictive than the first. We also assume that
   * if filtering fails, the request will have a runner that is a
   * {@link ErrorReportingRunner}. Luckily, we can cover this with tests to make
   * sure we don't break if JUnit changes in the future.
   *
   * @param request Request to filter
   * @param shardingFilter Sharding filter to use; {@link Filter#ALL} to not do sharding
   * @param testIncludeFilterRegexp String denoting a regular expression with which
   *     to filter tests.  Only test descriptions that match this regular
   *     expression will be run.  If {@code null}, tests will not be filtered.
   * @param testExcludeFilterRegexp String denoting a regular expression with which
   *     to filter tests.  Only test descriptions that do not match this regular
   *     expression will be run.  If {@code null}, tests will not be filtered.
   * @return Filtered request (may be a request that delegates to
   *         {@link ErrorReportingRunner}
   */
private static Request applyFilters(Request request, Filter shardingFilter, @Nullable String testIncludeFilterRegexp, @Nullable String testExcludeFilterRegexp) {
    // Allow the user to specify a filter on the command line
    boolean allowNoTests = false;
    Filter filter = Filter.ALL;
    if (testIncludeFilterRegexp != null) {
        filter = RegExTestCaseFilter.include(testIncludeFilterRegexp);
    }
    if (testExcludeFilterRegexp != null) {
        Filter excludeFilter = RegExTestCaseFilter.exclude(testExcludeFilterRegexp);
        filter = filter.intersect(excludeFilter);
    }
    if (testIncludeFilterRegexp != null || testExcludeFilterRegexp != null) {
        try {
            request = applyFilter(request, filter);
        } catch (NoTestsRemainException e) {
            return createErrorReportingRequestForFilterError(filter);
        }
        /*
       * If you filter a sharded test to run one test, we don't want all the
       * shards but one to fail.
       */
        allowNoTests = (shardingFilter != Filter.ALL);
    }
    // Sharding
    if (shardingFilter != Filter.ALL) {
        filter = filter.intersect(shardingFilter);
    }
    if (filter != Filter.ALL) {
        try {
            request = applyFilter(request, filter);
        } catch (NoTestsRemainException e) {
            if (allowNoTests) {
                return Request.runner(new NoOpRunner());
            } else {
                return createErrorReportingRequestForFilterError(filter);
            }
        }
    }
    return request;
}
Also used : Filter(org.junit.runner.manipulation.Filter) RegExTestCaseFilter(com.google.testing.junit.junit4.runner.RegExTestCaseFilter) SuiteTrimmingFilter(com.google.testing.junit.junit4.runner.SuiteTrimmingFilter) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Example 19 with Filter

use of org.junit.runner.manipulation.Filter in project bazel by bazelbuild.

the class ShardingFilterTestCase method simulateSelfRandomizingTestRun.

/**
   * Simulates test sharding with the given filters and test descriptions, for a
   * set of test descriptions that is in a different order in every test shard.
   *
   * @param filters a list of filters, one per test shard
   * @param descriptions a list of test descriptions
   * @return a mapping from each filter to the descriptions of the tests that would be run
   *   by the shard associated with that filter.
   */
protected static Map<Filter, List<Description>> simulateSelfRandomizingTestRun(List<Filter> filters, List<Description> descriptions) {
    if (descriptions.isEmpty()) {
        return new HashMap<>();
    }
    Deque<Description> mutatingDescriptions = new LinkedList<>(descriptions);
    Map<Filter, List<Description>> descriptionsRun = new HashMap<>();
    for (Filter filter : filters) {
        // rotate the queue so that each filter gets the descriptions in a different order
        mutatingDescriptions.addLast(mutatingDescriptions.pollFirst());
        for (Description description : descriptions) {
            if (filter.shouldRun(description)) {
                addDescriptionForFilterToMap(descriptionsRun, filter, description);
            }
        }
    }
    return descriptionsRun;
}
Also used : Description(org.junit.runner.Description) HashMap(java.util.HashMap) Filter(org.junit.runner.manipulation.Filter) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 20 with Filter

use of org.junit.runner.manipulation.Filter in project bazel by bazelbuild.

the class ShardingFilterTestCase method testShouldRunTestSuite.

public final void testShouldRunTestSuite() {
    Description testSuiteDescription = createTestSuiteDescription();
    Filter filter = createShardingFilterFactory().createFilter(TEST_DESCRIPTIONS, 0, 1);
    assertTrue(filter.shouldRun(testSuiteDescription));
}
Also used : Description(org.junit.runner.Description) Filter(org.junit.runner.manipulation.Filter)

Aggregations

Filter (org.junit.runner.manipulation.Filter)34 Test (org.junit.Test)14 Description (org.junit.runner.Description)14 Runner (org.junit.runner.Runner)8 Request (org.junit.runner.Request)7 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)7 Filterable (org.junit.runner.manipulation.Filterable)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 RunNotifier (org.junit.runner.notification.RunNotifier)3 RegExTestCaseFilter (com.google.testing.junit.junit4.runner.RegExTestCaseFilter)2 SuiteTrimmingFilter (com.google.testing.junit.junit4.runner.SuiteTrimmingFilter)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 TestSelectionMatcher (org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher)2 Test (org.junit.jupiter.api.Test)2 JUnitCore (org.junit.runner.JUnitCore)2 Result (org.junit.runner.Result)2 RunListener (org.junit.runner.notification.RunListener)2 MethodGlobFilter (com.carrotsearch.randomizedtesting.MethodGlobFilter)1