Search in sources :

Example 6 with NoTestsRemainException

use of org.junit.runner.manipulation.NoTestsRemainException in project gradle by gradle.

the class JUnitTestClassExecuter method runTestClass.

private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
    List<Filter> filters = new ArrayList<Filter>();
    if (options.hasCategoryConfiguration()) {
        verifyJUnitCategorySupport();
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {

            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        filters.add(new CategoryFilter(CollectionUtils.collect(options.getIncludeCategories(), transformer), CollectionUtils.collect(options.getExcludeCategories(), transformer)));
    }
    Request request = Request.aClass(testClass);
    Runner runner = request.getRunner();
    if (!options.getIncludedTests().isEmpty()) {
        TestSelectionMatcher matcher = new TestSelectionMatcher(options.getIncludedTests());
        // matches the filter, run the entire suite instead of filtering away its contents.
        if (!runner.getDescription().isSuite() || !matcher.matchesTest(testClassName, null)) {
            filters.add(new MethodNameFilter(matcher));
        }
    }
    if (runner instanceof Filterable) {
        Filterable filterable = (Filterable) runner;
        for (Filter filter : filters) {
            try {
                filterable.filter(filter);
            } catch (NoTestsRemainException e) {
                // Ignore
                return;
            }
        }
    } else if (allTestsFiltered(runner, filters)) {
        return;
    }
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);
    runner.run(notifier);
}
Also used : Runner(org.junit.runner.Runner) Transformer(org.gradle.api.Transformer) RunNotifier(org.junit.runner.notification.RunNotifier) ArrayList(java.util.ArrayList) Request(org.junit.runner.Request) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) TestSelectionMatcher(org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher) Filter(org.junit.runner.manipulation.Filter) InvalidUserDataException(org.gradle.api.InvalidUserDataException) Filterable(org.junit.runner.manipulation.Filterable)

Example 7 with NoTestsRemainException

use of org.junit.runner.manipulation.NoTestsRemainException 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 8 with NoTestsRemainException

use of org.junit.runner.manipulation.NoTestsRemainException in project randomizedtesting by randomizedtesting.

the class SlaveMain method execute.

/**
   * Execute tests.
   */
private void execute(Iterator<String> classNames) throws Throwable {
    final RunNotifier fNotifier = new OrderedRunNotifier();
    final Result result = new Result();
    final Writer debug = debugMessagesFile == null ? new NullWriter() : new OutputStreamWriter(new FileOutputStream(debugMessagesFile), "UTF-8");
    fNotifier.addListener(result.createListener());
    fNotifier.addListener(new StreamFlusherDecorator(new NoExceptionRunListenerDecorator(new RunListenerEmitter(serializer)) {

        @Override
        protected void exception(Throwable t) {
            warn("Event serializer exception.", t);
        }
    }));
    fNotifier.addListener(new RunListener() {

        public void testRunFinished(Result result) throws Exception {
            debug(debug, "testRunFinished(T:" + result.getRunCount() + ";F:" + result.getFailureCount() + ";I:" + result.getIgnoreCount() + ")");
            serializer.flush();
        }

        @Override
        public void testRunStarted(Description description) throws Exception {
            debug(debug, "testRunStarted(" + description + ")");
            serializer.flush();
        }

        @Override
        public void testStarted(Description description) throws Exception {
            debug(debug, "testStarted(" + description + ")");
            serializer.flush();
        }

        public void testFinished(Description description) throws Exception {
            debug(debug, "testFinished(" + description + ")");
            serializer.flush();
        }

        @Override
        public void testIgnored(Description description) throws Exception {
            debug(debug, "testIgnored(T:" + description + ")");
        }

        @Override
        public void testFailure(Failure failure) throws Exception {
            debug(debug, "testFailure(T:" + failure + ")");
        }

        @Override
        public void testAssumptionFailure(Failure failure) {
            try {
                debug(debug, "testAssumptionFailure(T:" + failure + ")");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    /*
     * Instantiate method filter if any.
     */
    String methodFilterGlob = Strings.emptyToNull(System.getProperty(SysGlobals.SYSPROP_TESTMETHOD()));
    Filter methodFilter = Filter.ALL;
    if (methodFilterGlob != null) {
        methodFilter = new MethodGlobFilter(methodFilterGlob);
    }
    /*
     * Important. Run each class separately so that we get separate 
     * {@link RunListener} callbacks for the top extracted description.
     */
    debug(debug, "Entering main suite loop.");
    try {
        while (classNames.hasNext()) {
            final String clName = classNames.next();
            debug(debug, "Instantiating: " + clName);
            Class<?> clazz = instantiate(clName);
            if (clazz == null)
                continue;
            Request request = Request.aClass(clazz);
            try {
                Runner runner = request.getRunner();
                methodFilter.apply(runner);
                fNotifier.fireTestRunStarted(runner.getDescription());
                debug(debug, "Runner.run(" + clName + ")");
                runner.run(fNotifier);
                debug(debug, "Runner.done(" + clName + ")");
                fNotifier.fireTestRunFinished(result);
            } catch (NoTestsRemainException e) {
            // Don't complain if all methods have been filtered out. 
            // I don't understand the reason why this exception has been
            // built in to filters at all.
            }
        }
    } catch (Throwable t) {
        debug(debug, "Main suite loop error: " + t);
        throw t;
    } finally {
        debug(debug, "Leaving main suite loop.");
        debug.close();
    }
}
Also used : Runner(org.junit.runner.Runner) Description(org.junit.runner.Description) MethodGlobFilter(com.carrotsearch.randomizedtesting.MethodGlobFilter) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) Result(org.junit.runner.Result) Failure(org.junit.runner.notification.Failure) RunNotifier(org.junit.runner.notification.RunNotifier) Request(org.junit.runner.Request) IOException(java.io.IOException) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) RunListener(org.junit.runner.notification.RunListener) MethodGlobFilter(com.carrotsearch.randomizedtesting.MethodGlobFilter) Filter(org.junit.runner.manipulation.Filter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 9 with NoTestsRemainException

use of org.junit.runner.manipulation.NoTestsRemainException in project pinpoint by naver.

the class PinpointPluginTestSuite method filter.

@Override
public void filter(Filter filter) throws NoTestsRemainException {
    synchronized (childrenLock) {
        List<Runner> children = new ArrayList<Runner>(getFilteredChildren());
        for (Iterator<Runner> iter = children.iterator(); iter.hasNext(); ) {
            Runner each = iter.next();
            if (shouldRun(filter, each)) {
                try {
                    filter.apply(each);
                } catch (NoTestsRemainException e) {
                    iter.remove();
                }
            } else {
                iter.remove();
            }
        }
        filteredChildren = Collections.unmodifiableCollection(children);
        if (filteredChildren.isEmpty()) {
            throw new NoTestsRemainException();
        }
    }
}
Also used : Runner(org.junit.runner.Runner) ArrayList(java.util.ArrayList) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Example 10 with NoTestsRemainException

use of org.junit.runner.manipulation.NoTestsRemainException in project intellij-community by JetBrains.

the class TestAll method getTest.

@Nullable
private static Test getTest(@NotNull final Class<?> testCaseClass) {
    try {
        if ((testCaseClass.getModifiers() & Modifier.PUBLIC) == 0) {
            return null;
        }
        Bombed classBomb = testCaseClass.getAnnotation(Bombed.class);
        if (classBomb != null && PlatformTestUtil.bombExplodes(classBomb)) {
            return new ExplodedBomb(testCaseClass.getName(), classBomb);
        }
        Method suiteMethod = safeFindMethod(testCaseClass, "suite");
        if (suiteMethod != null && !isPerformanceTestsRun()) {
            return (Test) suiteMethod.invoke(null, ArrayUtil.EMPTY_CLASS_ARRAY);
        }
        if (TestRunnerUtil.isJUnit4TestClass(testCaseClass)) {
            JUnit4TestAdapter adapter = new JUnit4TestAdapter(testCaseClass);
            boolean runEverything = isIncludingPerformanceTestsRun() || isPerformanceTest(null, testCaseClass) && isPerformanceTestsRun();
            if (!runEverything) {
                try {
                    adapter.filter(isPerformanceTestsRun() ? PERFORMANCE_ONLY : NO_PERFORMANCE);
                } catch (NoTestsRemainException ignored) {
                }
            }
            return adapter;
        }
        final int[] testsCount = { 0 };
        TestSuite suite = new TestSuite(testCaseClass) {

            @Override
            public void addTest(Test test) {
                if (!(test instanceof TestCase)) {
                    doAddTest(test);
                } else {
                    String name = ((TestCase) test).getName();
                    // Mute TestSuite's "no tests found" warning
                    if ("warning".equals(name))
                        return;
                    if (!isIncludingPerformanceTestsRun() && (isPerformanceTestsRun() ^ isPerformanceTest(name, testCaseClass)))
                        return;
                    Method method = findTestMethod((TestCase) test);
                    if (method == null) {
                        doAddTest(test);
                    } else {
                        Bombed methodBomb = method.getAnnotation(Bombed.class);
                        if (methodBomb == null) {
                            doAddTest(test);
                        } else if (PlatformTestUtil.bombExplodes(methodBomb)) {
                            doAddTest(new ExplodedBomb(method.getDeclaringClass().getName() + "." + method.getName(), methodBomb));
                        }
                    }
                }
            }

            private void doAddTest(Test test) {
                testsCount[0]++;
                super.addTest(test);
            }

            @Nullable
            private Method findTestMethod(final TestCase testCase) {
                return safeFindMethod(testCase.getClass(), testCase.getName());
            }
        };
        return testsCount[0] > 0 ? suite : null;
    } catch (Throwable t) {
        System.err.println("Failed to load test: " + testCaseClass.getName());
        t.printStackTrace(System.err);
        return null;
    }
}
Also used : NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) Method(java.lang.reflect.Method) Bombed(com.intellij.idea.Bombed) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)11 Runner (org.junit.runner.Runner)6 Filter (org.junit.runner.manipulation.Filter)6 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 Description (org.junit.runner.Description)2 Request (org.junit.runner.Request)2 Filterable (org.junit.runner.manipulation.Filterable)2 RunNotifier (org.junit.runner.notification.RunNotifier)2 MethodGlobFilter (com.carrotsearch.randomizedtesting.MethodGlobFilter)1 RegExTestCaseFilter (com.google.testing.junit.junit4.runner.RegExTestCaseFilter)1 SuiteTrimmingFilter (com.google.testing.junit.junit4.runner.SuiteTrimmingFilter)1 Bombed (com.intellij.idea.Bombed)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 Pattern (java.util.regex.Pattern)1 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)1