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