String runTests() { PrintStream sysOut = System.out; PrintStream sysErr = System.err; ByteArrayOutputStream stream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(stream); try { System.setOut(out); System.setErr(out); TextListener listener = new TextListener(out); JUnitCore junit = new JUnitCore(); junit.addListener(listener); junit.run(MyClassIT.class, AnotherClassIT.class, ...etc...); } finally { System.setOut(sysOut); System.setErr(sysErr); out.close(); } return stream.toString(); }
/** * Run the tests contained in <code>classes</code>. Write feedback while the tests * are running and write stack traces for all failed tests after all tests complete. This is * similar to {@link #main(String[])}, but intended to be used programmatically. * * @param classes Classes in which to find tests * @return a {@link Result} describing the details of the test run and the failed tests. */ public static Result runClasses(Class<?>... classes) { return runClasses(defaultComputer(), classes); }
/** * Run the tests contained in <code>classes</code>. Write feedback while the tests * are running and write stack traces for all failed tests after all tests complete. This is * similar to {@link #main(String[])}, but intended to be used programmatically. * * @param computer Helps construct Runners from classes * @param classes Classes in which to find tests * @return a {@link Result} describing the details of the test run and the failed tests. */ public static Result runClasses(Computer computer, Class<?>... classes) { return new JUnitCore().run(computer, classes); }
/** * Run all the tests in <code>classes</code>. * * @param classes the classes containing tests * @return a {@link Result} describing the details of the test run and the failed tests. */ public Result run(Class<?>... classes) { return run(defaultComputer(), classes); }
JUnitCore core = new JUnitCore(); core.addListener(new RL()); Request r = Request.aClass(testCase); return core.run(r.filterWith(new Filter() {
public Result execute() { Preconditions.checkState( isStarted.get(), "Cannot execute a tracker that has not started!" ); Result result = new JUnitCore().run( testClass ); long runTime = result.getRunTime(); // collect some statistics maxTime = Math.max( maxTime, runTime ); minTime = Math.min( minTime, runTime ); long timesRun = actualIterations.incrementAndGet(); long totalTime = totalRunTime.addAndGet( runTime ); totalTestsRun.addAndGet( result.getRunCount() ); meanTime = totalTime / timesRun; if ( ! result.wasSuccessful() ) { failures.addAndGet( result.getFailureCount() ); ignores.addAndGet( result.getIgnoreCount() ); } resultsLog.write( result ); return result; }
@Test public void testWorkingCategoryAndParameterized() { Request request = Request.aClass(WorkingCategoryClass.class); ExposedParameterized runner = (ExposedParameterized) request.getRunner(); request = request.filterWith(new CategoryFilter((ExposedGetAnnotations) runner.getChildren().get(0))); Result result = new JUnitCore().run(request); assertEquals(2, result.getRunCount()); }
@Override protected int doWork() throws Exception { //this is called from the command line, so we should set to use the distributed cluster IntegrationTestingUtility.setUseDistributedCluster(conf); Class<?>[] classes = findIntegrationTestClasses(); LOG.info("Found " + classes.length + " integration tests to run:"); for (Class<?> aClass : classes) { LOG.info(" " + aClass); } JUnitCore junit = new JUnitCore(); junit.addListener(new TextListener(System.out)); Result result = junit.run(classes); return result.wasSuccessful() ? 0 : 1; } }
RichLogger logger = new RichLogger(loggers, settings, testClassName); EventDispatcher ed = new EventDispatcher(logger, eventHandler, settings, fingerprint); JUnitCore ju = new JUnitCore(); ju.addListener(ed); if (runListener != null) { ju.addListener(createRunListener(runListener)); Class<?> cl = testClassLoader.loadClass(testClassName); if(shouldRun(fingerprint, cl, settings)) { Request request = Request.classes(cl); if(globPatterns.size() > 0) request = new SilentFilterRequest(request, new GlobFilter(settings, globPatterns)); if(testFilter.length() > 0) request = new SilentFilterRequest(request, new TestFilter(testFilter, ed)); loadClasses(testClassLoader, excludeCategories))); ju.run(request);
@Test public void testMethodRule() throws Exception { Result result = new JUnitCore().run(new VertxUnitRunner(MethodRuleTestSuite.class)); assertEquals(0, result.getFailures().size()); assertEquals(1, MethodRuleTestSuite.count.get()); assertEquals(0, MethodRuleTestSuite.rule.failures.size()); assertEquals(1, MethodRuleTestSuite.rule.methods.size()); assertEquals(MethodRuleTestSuite.class.getDeclaredMethod("testMethod", TestContext.class), MethodRuleTestSuite.rule.methods.get(0).getMethod()); assertEquals(1, MethodRuleTestSuite.rule.targets.size()); assertTrue(MethodRuleTestSuite.rule.targets.get(0) instanceof MethodRuleTestSuite); assertEquals(1, MethodRuleTestSuite.rule.evaluateCount.get()); }
/** * Runs main and exits */ private static void runMainAndExit(JUnitSystem system, String... args) { Result result = new JUnitCore().runMain(system, args); System.exit(result.wasSuccessful() ? 0 : 1); }
@Test public void testReportLateFailures() { Result result; try { result = new JUnitCore().run(new BlockJUnit4ClassRunner(LateFailureReport.class)); } catch (InitializationError initializationError) { throw new AssertionError(initializationError); } assertEquals(1, result.getFailureCount()); assertEquals(IllegalStateException.class, result.getFailures().get(0).getException().getClass()); } }
JUnitCore junit = new JUnitCore(); junit.addListener(new LogListener()); Result result = junit.run(tests.toArray(new Class[0])); if( result.getFailures().size() == 0 ) { Log.info("SUCCESS!"); System.exit(0);
/** * @param system * @args args from main() */ private Result runMain(JUnitSystem system, String... args) { system.out().println("JUnit version " + Version.id()); List<Class<?>> classes = new ArrayList<Class<?>>(); List<Failure> missingClasses = new ArrayList<Failure>(); for (String each : args) { try { classes.add(Class.forName(each)); } catch (ClassNotFoundException e) { system.out().println("Could not find class: " + each); Description description = Description.createSuiteDescription(each); Failure failure = new Failure(description, e); missingClasses.add(failure); } } RunListener listener = new TextListener(system); addListener(listener); Result result = run(classes.toArray(new Class[0])); for (Failure each : missingClasses) { result.getFailures().add(each); } return result; }
/** * Run all the tests contained in <code>request</code>. * * This variant should be used if {@code core} has attached listeners that this * run should notify. * * @param request the request describing tests * @param core a JUnitCore to delegate to. * @return a {@link Result} describing the details of the test run and the failed tests. */ public Result run(Request request, JUnitCore core) { core.addListener(history.listener()); return core.run(sortRequest(request).getRunner()); }
@Test @SneakyThrows public void run() { JUnitCore junit = new JUnitCore(); Result result = junit.run(SerializableComparableTest.class); if (!result.wasSuccessful()) { fail(Joiner.on("\n").join(result.getFailures())); } } }
private void verifyExceptionThrownByDiffAssertionRule(boolean shouldBeThrown, String methodName) { Request assertStateApi = Request.method(NoDiffAssertionRuleTest.class, methodName); Result result = new JUnitCore().run(assertStateApi); if (shouldBeThrown) { assertThat(result.getFailures()).hasSize(1); verifyExceptionAssertionErrorWithDiff(result.getFailures().get(0).getException()); } else { assertThat(result.getFailures()).isEmpty(); } }
@Override public Result call() throws Exception { JUnitCore runner = new JUnitCore(); runner.addListener(listener); Request request = Request.method(testClassFromCustomClassLoader(), testCaseName()); try { return runner.run(request); } catch (Throwable e) { throw new RuntimeException(e); } }
/** * Run the tests contained in <code>classes</code>. Write feedback while the tests * are running and write stack traces for all failed tests after all tests complete. This is * similar to {@link #main(String[])}, but intended to be used programmatically. * * @param classes Classes in which to find tests * @return a {@link Result} describing the details of the test run and the failed tests. */ public static Result runClasses(Class<?>... classes) { return new JUnitCore().run(defaultComputer(), classes); }