Search in sources :

Example 66 with Runner

use of org.junit.runner.Runner 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);
                // New RunListener instances should be added per class and then removed from the RunNotifier
                ArrayList<RunListener> runListenerInstances = instantiateRunListeners();
                for (RunListener runListener : runListenerInstances) {
                    fNotifier.addListener(runListener);
                }
                fNotifier.fireTestRunStarted(runner.getDescription());
                debug(debug, "Runner.run(" + clName + ")");
                runner.run(fNotifier);
                debug(debug, "Runner.done(" + clName + ")");
                fNotifier.fireTestRunFinished(result);
                for (RunListener runListener : runListenerInstances) {
                    fNotifier.removeListener(runListener);
                }
            } 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 : RandomizedRunner(com.carrotsearch.randomizedtesting.RandomizedRunner) 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 67 with Runner

use of org.junit.runner.Runner in project spring-framework by spring-projects.

the class JUnitTestingUtils method runTestsAndAssertCounters.

/**
 * Run the tests in the supplied {@code testClass}, using the specified
 * {@link Runner}, and assert the expectations of the test execution.
 *
 * <p>If the specified {@code runnerClass} is {@code null}, the tests
 * will be run with the runner that the test class is configured with
 * (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
 *
 * @param runnerClass the explicit runner class to use or {@code null}
 * if the default JUnit runner should be used
 * @param testClass the test class to run with JUnit
 * @param expectedStartedCount the expected number of tests that started
 * @param expectedFailedCount the expected number of tests that failed
 * @param expectedFinishedCount the expected number of tests that finished
 * @param expectedIgnoredCount the expected number of tests that were ignored
 * @param expectedAssumptionFailedCount the expected number of tests that
 * resulted in a failed assumption
 */
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass, int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount, int expectedAssumptionFailedCount) throws Exception {
    TrackingRunListener listener = new TrackingRunListener();
    if (runnerClass != null) {
        Constructor<?> constructor = runnerClass.getConstructor(Class.class);
        Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    } else {
        JUnitCore junit = new JUnitCore();
        junit.addListener(listener);
        junit.run(testClass);
    }
    assertSoftly(softly -> {
        softly.assertThat(listener.getTestStartedCount()).as("tests started for [%s]", testClass).isEqualTo(expectedStartedCount);
        softly.assertThat(listener.getTestFailureCount()).as("tests failed for [%s]", testClass).isEqualTo(expectedFailedCount);
        softly.assertThat(listener.getTestFinishedCount()).as("tests finished for [%s]", testClass).isEqualTo(expectedFinishedCount);
        softly.assertThat(listener.getTestIgnoredCount()).as("tests ignored for [%s]", testClass).isEqualTo(expectedIgnoredCount);
        softly.assertThat(listener.getTestAssumptionFailureCount()).as("failed assumptions for [%s]", testClass).isEqualTo(expectedAssumptionFailedCount);
    });
}
Also used : Runner(org.junit.runner.Runner) RunNotifier(org.junit.runner.notification.RunNotifier) JUnitCore(org.junit.runner.JUnitCore)

Example 68 with Runner

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

the class AnnotatedBuilderTest method topLevelTestClassWithoutAnnotation_isRunWithDefaultRunner.

@Test
public void topLevelTestClassWithoutAnnotation_isRunWithDefaultRunner() throws Exception {
    Runner runner = builder.runnerForClass(Object.class);
    assertThat(runner, is(nullValue()));
}
Also used : Runner(org.junit.runner.Runner) Test(org.junit.Test)

Example 69 with Runner

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

the class AnnotatedBuilderTest method memberClassDeepInsideAnnotatedTopLevelClass_isRunWithTopLevelRunner.

@Test
public void memberClassDeepInsideAnnotatedTopLevelClass_isRunWithTopLevelRunner() throws Exception {
    Runner runner = builder.runnerForClass(OuterClass.InnerClassWithoutOwnRunWith.MostInnerClass.class);
    assertThat(runner, is(instanceOf(RunnerSpy.class)));
    RunnerSpy runnerSpy = (RunnerSpy) runner;
    assertThat(runnerSpy.getInvokedTestClass(), is((Object) OuterClass.InnerClassWithoutOwnRunWith.MostInnerClass.class));
}
Also used : Runner(org.junit.runner.Runner) RunnerSpy(org.junit.runner.RunnerSpy) Test(org.junit.Test)

Example 70 with Runner

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

the class AnnotatedBuilderTest method memberClassInsideAnnotatedTopLevelClass_isRunWithTopLevelRunner.

@Test
public void memberClassInsideAnnotatedTopLevelClass_isRunWithTopLevelRunner() throws Exception {
    Runner runner = builder.runnerForClass(OuterClass.InnerClassWithoutOwnRunWith.class);
    assertThat(runner, is(instanceOf(RunnerSpy.class)));
    RunnerSpy runnerSpy = (RunnerSpy) runner;
    assertThat(runnerSpy.getInvokedTestClass(), is((Object) OuterClass.InnerClassWithoutOwnRunWith.class));
}
Also used : Runner(org.junit.runner.Runner) RunnerSpy(org.junit.runner.RunnerSpy) Test(org.junit.Test)

Aggregations

Runner (org.junit.runner.Runner)106 Test (org.junit.Test)39 RunNotifier (org.junit.runner.notification.RunNotifier)22 ArrayList (java.util.ArrayList)18 Description (org.junit.runner.Description)18 JUnitCore (org.junit.runner.JUnitCore)18 ParentRunner (org.junit.runners.ParentRunner)16 Request (org.junit.runner.Request)15 Result (org.junit.runner.Result)13 Filter (org.junit.runner.manipulation.Filter)12 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)12 InitializationError (org.junit.runners.model.InitializationError)10 Method (java.lang.reflect.Method)9 JUnit4ParameterizedTest (androidx.test.testing.fixtures.JUnit4ParameterizedTest)7 SuiteConfiguration (org.eclipse.reddeer.junit.internal.configuration.SuiteConfiguration)7 Failure (org.junit.runner.notification.Failure)7 ErrorReportingRunner (org.junit.internal.runners.ErrorReportingRunner)6 Filterable (org.junit.runner.manipulation.Filterable)6 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)6 Field (java.lang.reflect.Field)5