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;
}
});
}
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));
}
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;
}
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;
}
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));
}
Aggregations