Java Code Examples for org.junit.runner.Result#getFailureCount()

The following examples show how to use org.junit.runner.Result#getFailureCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericRunner.java    From vividus with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    Class<?> clazz = MethodHandles.lookup().lookupClass();
    Result result = new JUnitCore().run(clazz);
    int exitCode;
    if (result.getFailureCount() > 0)
    {
        Logger logger = LoggerFactory.getLogger(clazz);
        result.getFailures().forEach(f ->
        {
            logger.error("Failure: {}", f);
            logger.error(f.getTrace());
        });
        exitCode = ERROR_EXIT_CODE;
    }
    else
    {
        exitCode = calculateExitCode(BeanFactory.getBean(IRunStatusProvider.class).getRunStatus());
    }
    System.exit(exitCode);
}
 
Example 2
Source File: JUnitRunner.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  try {
    H2O.main(args);
    TestUtil.stall_till_cloudsize(3);
    List<Class> tests = JUnitRunner.all();
    Result r = org.junit.runner.JUnitCore.runClasses(tests.toArray(new Class[0]));
    if( r.getFailureCount() == 0 ) {
      System.out.println("Successfully ran the following tests in " + (r.getRunTime() / 1000) + "s");
      for( Class c : tests )
        System.out.println(c.getName());
    } else {
      for( Failure f : r.getFailures() ) {
        System.err.println(f.getDescription());
        if( f.getException() != null )
          f.getException().printStackTrace();
      }
    }
    System.exit(r.getFailureCount());
  } catch( Throwable t ) {
    t.printStackTrace();
    System.exit(1);
  }
}
 
Example 3
Source File: TestRunner.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the current state of <code>this</code> with the <code>result</code>.
 *
 * @param testClass The test class executed.
 * @param result The results of the test class execution.
 * @return <code>this</code>
 */
public TestResults merge(Class<?> testClass, Result result) {
  LOG.info("Tests run: {}, Failures: {}, Skipped: {}, Time elapsed: {} - in {}",
      result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
      TimeUnit.SECONDS.convert(result.getRunTime(), TimeUnit.MILLISECONDS),
      testClass.getName());

  numRun += result.getRunCount();
  numFailed += result.getFailureCount();
  numIgnored += result.getIgnoreCount();

  // Collect the failures
  if (!result.wasSuccessful()) {
    failures.addAll(result.getFailures());
  }

  return this;
}
 
Example 4
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 6 votes vote down vote up
protected boolean runBootstrapTest(RunNotifier notifier, TestClass testClass) {
    if (!runningBootstrapTest.get().booleanValue()) {
        runningBootstrapTest.set(Boolean.TRUE);
        try {
            BootstrapTest bootstrapTest = getBootstrapTestAnnotation(testClass.getJavaClass());
            if (bootstrapTest != null) {
                Result result = JUnitCore.runClasses(bootstrapTest.value());
                List<Failure> failures = result.getFailures();
                for (Failure failure : failures) {
                    notifier.fireTestFailure(failure);
                }
                return result.getFailureCount() == 0;
            } else {
                throw new IllegalStateException("LoadTimeWeavableTestRunner, must be coupled with an @BootstrapTest annotation to define the bootstrap test to execute.");
            }
        } finally {
            runningBootstrapTest.set(Boolean.FALSE);
        }
    }
    return true;
}
 
Example 5
Source File: JUnit4RunnerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testShardingIsSupported() {
  config = createConfig();
  shardingEnvironment = mock(ShardingEnvironment.class);
  shardingFilters = new FakeShardingFilters(
      Description.createTestDescription(SamplePassingTest.class, "testThatAlwaysPasses"),
      Description.createTestDescription(SampleFailingTest.class, "testThatAlwaysFails"));

  when(shardingEnvironment.isShardingEnabled()).thenReturn(true);

  JUnit4Runner runner = createRunner(SampleSuite.class);
  Result result = runner.run();

  verify(shardingEnvironment).touchShardFile();

  assertThat(result.getRunCount()).isEqualTo(2);
  if (result.getFailureCount() > 1) {
    fail("Too many failures: " + result.getFailures());
  }
  assertThat(result.getFailureCount()).isEqualTo(1);
  assertThat(result.getIgnoreCount()).isEqualTo(0);
  assertThat(runner.getModel().getNumTestCases()).isEqualTo(2);
}
 
Example 6
Source File: JUnitExternalExecutor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public String createOutput(Result r) {
	String out = "[";
	int count = 0;
	int failures = 0;
	try {
		for (Failure f : r.getFailures()) {
			String s = failureMessage(f);
			if (!s.startsWith("warning")) {
				failures++;
			}
			out += s + "-,";
			count++;
			if (count > 10) {
				out += "...and " + (r.getFailureCount() - 10) + " failures more,";
				// break;
			}
		}
	} catch (Exception e) {
		// We do not care about this exception,
	}
	out = out + "]";
	return (OUTSEP + r.getRunCount() + OUTSEP + failures + OUTSEP + out + OUTSEP);
}
 
Example 7
Source File: ConstraintModelBuilder.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private boolean isSynthesisPossible(final Result firstResultWithFalse, final Result secondResultWithTrue) {
  	// this method is a key optimization in Nopol
// it enables to not start the synthesis when we are sure that it is not possible
// only based on the observed test outcomes with angelic values

      Collection<Description> testFailuresWithFalse = TestSuiteExecution.collectDescription(firstResultWithFalse.getFailures());
      Collection<Description> testFailuresWithTrue = TestSuiteExecution.collectDescription(secondResultWithTrue.getFailures());

      // contract: all test failures must either in testFailuresWithFalse or in secondFailures
// removes from testFailuresWithFalse all of its elements that are not contained in testFailuresWithTrue
// consequently, it remains the elements that are always failing
Collection<Description> testsThatAreAlwasyFailing = new ArrayList<>(testFailuresWithFalse);
testsThatAreAlwasyFailing.retainAll(testFailuresWithTrue);

      boolean synthesisIsPossible = testsThatAreAlwasyFailing.isEmpty();

      // some logging
      int nbFirstSuccess = firstResultWithFalse.getRunCount() - firstResultWithFalse.getFailureCount();
      int nbSecondSuccess = secondResultWithTrue.getRunCount() - secondResultWithTrue.getFailureCount();
      if (!synthesisIsPossible || (nbFirstSuccess == 0 && nbSecondSuccess == 0)) {
          Logger testsOutput = LoggerFactory.getLogger("tests.output");
          testsOutput.debug("Failing tests with false: \n{}", firstResultWithFalse.getFailures());
          testsOutput.debug("Failing tests with true: \n{}", secondResultWithTrue.getFailures());
      }
      return synthesisIsPossible;
  }
 
Example 8
Source File: SquidbTestRunner.java    From squidb with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the test classes given in {@param classes}.
 *
 * @returns Zero if all tests pass, non-zero otherwise.
 */
public static int run(Class[] classes, RunListener listener, PrintStream out) {
    JUnitCore junitCore = new JUnitCore();
    junitCore.addListener(listener);
    boolean hasError = false;
    int numTests = 0;
    int numFailures = 0;
    long start = System.currentTimeMillis();
    for (@AutoreleasePool Class c : classes) {
        out.println("Running " + c.getName());
        Result result = junitCore.run(c);
        numTests += result.getRunCount();
        numFailures += result.getFailureCount();
        hasError = hasError || !result.wasSuccessful();
    }
    long end = System.currentTimeMillis();
    out.println(String.format("Ran %d tests, %d failures. Total time: %s seconds", numTests, numFailures,
            NumberFormat.getInstance().format((double) (end - start) / 1000)));
    return hasError ? 1 : 0;
}
 
Example 9
Source File: TraversalsTests.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public static void main(String []argv)
{
	int maxIterations = 10;
    JUnitCore junit = new JUnitCore();
    Result result = null;
    int iter = 0;
    do
    {
    	result = junit.run(Request.method(TraversalsTests.class, "testDepthFirstOnCircle"));
    	iter++;
    	System.out.println("Failures " + result.getFailureCount());
     if (result.getFailureCount() > 0)
     {
         for (Failure failure : result.getFailures())
         {
             failure.getException().printStackTrace();
         }
     }
    } while (result.getFailureCount() == 0 && iter < maxIterations);
}
 
Example 10
Source File: PropertyIndexingTests.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public static void main(String []argv)
{
	JUnitCore junit = new JUnitCore();
	Result result = null;
	do
	{
		result = junit.run(Request.method(PropertyIndexingTests.class, "derivedPropertyIndexing"));
	} while (result.getFailureCount() == 0 && false);
	System.out.println("Failures " + result.getFailureCount());
	if (result.getFailureCount() > 0)
	{
		for (Failure failure : result.getFailures())
		{
			failure.getException().printStackTrace();
		}
	}
	System.exit(0);
}
 
Example 11
Source File: TestRunner.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
@Test
public void test() {
    Request request = Request.method(testMethod.getDeclaringClass(), testMethod.getName());
    Result result = new JUnitCore().run(request);
    if (result.getIgnoreCount() > 0)
        throw new AssumptionViolatedException("Test " + testMethod.getDeclaringClass()
                + "." + testMethod.getName() + " were ignored");
    if (result.getFailureCount() > 0) {
        Assert.fail(result.getFailures().toString());
    }
}
 
Example 12
Source File: TestSuiteExecutor.java    From dekaf with Apache License 2.0 5 votes vote down vote up
public static void run(final Class... suites) {

    boolean underTC = System.getenv(TEAMCITY_DETECT_VAR_NAME) != null;

    // prepare junit
    JUnitSystem system = new RealSystem();
    JUnitCore core = new JUnitCore();
    RunListener listener = underTC ? new TeamCityListener() : new TextListener(system);
    core.addListener(listener);

    int success = 0,
        failures = 0,
        ignores = 0;

    // run tests
    for (Class suite : suites) {
      sayNothing();
      String suiteName = suite.getSimpleName();
      if (suiteName.endsWith("Tests")) suiteName = suiteName.substring(0, suiteName.length()-"Tests".length());
      if (suiteName.endsWith("Integration")) suiteName = suiteName.substring(0, suiteName.length()-"Integration".length());
      if (suiteParameter != null) suiteName = suiteName + '[' + suiteParameter + ']';

      if (underTC) say("##teamcity[testSuiteStarted name='%s']", suiteName);

      Result result = core.run(suite);

      success += result.getRunCount() - (result.getFailureCount() + result.getIgnoreCount());
      failures += result.getFailureCount();
      ignores += result.getIgnoreCount();

      if (underTC) say("##teamcity[testSuiteFinished name='%s']", suiteName);
      sayNothing();
    }
  }
 
Example 13
Source File: TestParser.java    From mjson with Apache License 2.0 5 votes vote down vote up
public static void main(String [] argv)
{
	JUnitCore junit = new JUnitCore();
	Result result = null;
	result = junit.run(Request.method(TestParser.class, "malformedTest4"));
	if (result.getFailureCount() > 0)
	{
		for (Failure failure : result.getFailures())
		{
			failure.getException().printStackTrace();
		}
	}
	System.exit(0);
	
}
 
Example 14
Source File: JPFRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
private boolean executeTestAndCollectRuntimeValues(Object result,
												   TestCase currentTest, ClassLoader unitTestClassLoader,
												   SpecificationTestCasesListener<T> listener) {

	LoggingInstrumenter.setValue(result);
	Result testResult = TestSuiteExecution.runTestCase(currentTest,
			unitTestClassLoader, listener, nopolContext);
	if (testResult.getFailureCount() == 0) {
		return true;
	}
	return false;
}
 
Example 15
Source File: MutantSearchSpaceExplorator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
private static void checkThatAllTestsPass(Class<?> TEST_CLASS) {
	resetAllSelectors();
	JUnitCore core = new JUnitCore();
	Result result = core.run(TEST_CLASS);
	if (result.getFailureCount()>0) {
		// oops the tests are not valid before
		throw new RuntimeException("oops it does not pass anymore", result.getFailures().get(0).getException());
	}
}
 
Example 16
Source File: WithNestedTests.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void assertFailureCount(int expected, Result result) {
  if (result.getFailureCount() != expected) {
    StringBuilder b = new StringBuilder();
    for (Failure f : result.getFailures()) {
      b.append("\n\n");
      b.append(f.getMessage());
      b.append("\n");
      b.append(f.getTrace());
    }
    Assert.assertFalse("Expected failures: " + expected + " but was " + 
        result.getFailureCount() + ", failures below: " + b.toString(), true);
  }
}
 
Example 17
Source File: Repair.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void computeMethodCoverage() throws IOException{
	JUnitRuntime runtime = new JUnitRuntime(_subject);
	String src = _subject.getHome() + _subject.getSsrc();
	MethodInstrumentVisitor methodInstrumentVisitor = new MethodInstrumentVisitor();
	Instrument.execute(src, methodInstrumentVisitor);
	
	if(!Runner.compileSubject(_subject)){
		System.err.println("Build project failed!");
		System.exit(0);
	}
	
	System.out.println("Passed test classes : " + _localization.getPassedTestCases().size());
	for(String test : _localization.getPassedTestCases()){
		String[] testStr = test.split("#");
		String clazz = testStr[0];
		String methodName = testStr[1];
		OutStream outStream = new OutStream();
		Result result = JUnitEngine.getInstance(runtime).test(clazz, methodName, new PrintStream(outStream));
		if(result.getFailureCount() > 0){
			System.out.println("Error : Passed test cases running failed ! => " + clazz);
			System.exit(0);
		}
		for(Integer method : outStream.getOut()){
			Set<Pair<String, String>> tcases = _passedTestCasesMap.get(method);
			if(tcases == null){
				tcases = new HashSet<>();
			}
			tcases.add(new Pair<String, String>(clazz, methodName));
			_passedTestCasesMap.put(method, tcases);
		}
	}
	// restore source file
	_subject.restore();
}
 
Example 18
Source File: TestResult.java    From lambda-selenium with MIT License 5 votes vote down vote up
public TestResult(Result result) {
    runCount = result.getRunCount();
    failureCount = result.getFailureCount();
    ignoreCount = result.getIgnoreCount();
    runTime = result.getRunTime();
    if (!wasSuccessful()) {
        throwable = result.getFailures().get(0).getException();
    }
}
 
Example 19
Source File: UnitTestResult.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
public UnitTestResult(Result jr) {
    this.runCount = jr.getRunCount();
    this.failureCount = jr.getFailureCount();
    this.runTime = jr.getRunTime();
    this.ignoreCount = jr.getIgnoreCount();
    this.successful = jr.wasSuccessful();
}
 
Example 20
Source File: ForkedPinpointPluginTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private static int getFailureCount(Result result) {
    if (result == null) {
        return -1;
    }
    return result.getFailureCount();
}