下面列出了怎么用org.junit.runner.Result的API类实例代码及写法,或者点击链接到github查看源代码。
private static void runInternal(final RunNotifier notifier) {
if (openssl && OPENSSL_FAILURE != null) {
throw new RuntimeException(OPENSSL_FAILURE);
}
if (first) {
first = false;
undertow = Undertow.builder()
.setHandler(rootHandler)
.addHttpListener(getHostPort(DEFAULT), getHostAddress(DEFAULT))
.build();
undertow.start();
notifier.addListener(new RunListener() {
@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);
undertow.stop();
clientGroup.shutdownGracefully();
}
});
}
}
@Test
public void fromResult_failure() throws Exception {
Class<SampleJUnitTest> testClass = SampleJUnitTest.class;
Description jUnitDescription = Description.createTestDescription(testClass, "sampleTest");
Throwable throwable = new RuntimeException("Your test is bad and you should feel bad.");
Failure jUnitFailure = new Failure(jUnitDescription, throwable);
Result jUnitResult = new Result();
RunListener jUnitListener = jUnitResult.createListener();
jUnitListener.testRunStarted(jUnitDescription);
jUnitListener.testStarted(jUnitDescription);
jUnitListener.testFailure(jUnitFailure);
jUnitListener.testFinished(jUnitDescription);
ParcelableResult parcelableResult =
BundleJUnitUtils.getResult(parcelBundle(BundleJUnitUtils.getBundleFromResult(jUnitResult)));
assertThat(parcelableResult.wasSuccessful(), is(jUnitResult.wasSuccessful()));
assertThat(parcelableResult.getFailureCount(), is(jUnitResult.getFailureCount()));
compareFailure(parcelableResult.getFailures().get(0), jUnitResult.getFailures().get(0));
}
DelegateRunNotifier(Runner runner, RunNotifier delegate, long defaultTestTimeoutMillis) {
this.runner = runner;
this.delegate = delegate;
this.finishedTests = new HashSet<Description>();
this.defaultTestTimeoutMillis = defaultTestTimeoutMillis;
this.timer = new Timer();
this.hasTestThatExceededTimeout = new AtomicBoolean(false);
// Because our fireTestRunFinished() does not seem to get invoked, we listen for the
// delegate to fire a testRunFinished event so we can dispose of the timer.
delegate.addListener(
new RunListener() {
@Override
public void testRunFinished(Result result) {
onTestRunFinished();
}
});
}
public Outcome(Result result, String digest_) {
// Constructs an Outcome from a JUnit result.
// Only PASS/FAIL outcomes can be made this way: if a test times out
// or crashes the JVM, there won't be a JUnit result to base the Outcome on.
// In those cases, use createTimeout or createCrash.
digest = digest_;
coveredMutants = new ArrayList<Integer>();
if (result.wasSuccessful()) {
type = Type.PASS;
runTime = result.getRunTime();
stackTrace = "";
} else {
type = Type.FAIL;
runTime = result.getRunTime();
stackTrace = normalizeStackTrace(result.getFailures().get(0).getTrace());
}
}
/**
* Can be overridden to customize the failure string message.
*/
protected @NonNull
String buildLeakDetectedMessage(
@NonNull List<InstrumentationLeakResults.Result> detectedLeaks) {
StringBuilder failureMessage = new StringBuilder();
failureMessage.append(
"Test failed because memory leaks were detected, see leak traces below.\n");
failureMessage.append(SEPARATOR);
Context context = getInstrumentation().getContext();
for (InstrumentationLeakResults.Result detectedLeak : detectedLeaks) {
failureMessage.append(
LeakCanary.leakInfo(context, detectedLeak.heapDump, detectedLeak.analysisResult, true));
failureMessage.append(SEPARATOR);
}
return failureMessage.toString();
}
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);
}
private void handleNoRunListenerReport(ZeroCodeTestReportListener reportListener) {
if (CHARTS_AND_CSV.equals(getProperty(ZEROCODE_JUNIT))) {
/**
* Gradle does not support JUnit RunListener. Hence Zerocode gracefully handled this
* upon request from Gradle users. But this is not limited to Gradle, anywhere you
* want to bypass the JUnit RunListener, you can achieve this way.
* See README for details.
*
* There are number of tickets opened for this, but not yet fixed.
* - https://discuss.gradle.org/t/testrunfinished-not-run-in-junit-integration/14644
* - https://github.com/gradle/gradle/issues/842
* - many more related tickets.
*/
LOGGER.debug("Bypassed JUnit RunListener [as configured by the build tool] to generate useful reports...");
reportListener.testRunFinished(new Result());
}
}
/**
* this function launch a parallel class
* @param classes
* @param core
* @return Core Result or null on blocking
*/
public static Result runWithThread(Class<?> classes, JUnitCore core) {
int i = 0;
// Define Runner
RunnerThreaded runner = new RunnerThreaded(classes,core);
runner.start();
try {
runner.join();
} catch (InterruptedException e) {
throw new RuntimeException("interrupted");
}
if (runner.getResult()==null) {
throw new RuntimeException("interrupted");
}
return runner.getResult();
}
private void logTestRunFinished(Result result) {
Collection<String> lines = MetaList.newArrayList();
lines.add(format("Tests run finished (%d ms)", result.getRunTime()));
lines.add("<> Total tests run: " + result.getRunCount());
lines.add("<> Ignored tests: " + result.getIgnoreCount());
lines.add("<> Failed tests: " + result.getFailureCount());
for (Failure failure : result.getFailures()) {
lines.add("~ " + failure.getTestHeader());
lines.add("[" + failure.getMessage() + "]");
Throwable exception = failure.getException();
lines.add(exception.toString());
for (int i = 0; i <= 5; i += 1) {
StackTraceElement element = exception.getStackTrace()[i];
lines.add(" at " + element.toString());
}
}
//logDebug(logger(), lines);
}
/** Test provided both include and exclude annotations. */
@Test
public void testTestSizeFilter_annotationAndNotAnnotationAtMethod() {
Request request =
builder
.addAnnotationInclusionFilter(SmallTest.class.getName())
.addAnnotationExclusionFilter(FlakyTest.class.getName())
.addTestClass(SampleRunnerFilterSizeTest.class.getName())
.addTestClass(SampleMultipleAnnotation.class.getName())
.build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
// expect 1 test that passed.
Assert.assertEquals(1, result.getRunCount());
Assert.assertEquals(0, result.getFailureCount());
}
/**
* Test case where method has both a size annotation and suppress annotation. Expect suppress to
* overrule the size.
*/
@Test
public void testSizeWithSuppress() {
Request request =
builder
.addTestClass(SampleSizeWithSuppress.class.getName())
.addTestClass(SampleJUnit3Test.class.getName())
.addTestSizeFilter(TestSize.SMALL)
.build();
JUnitCore testRunner = new JUnitCore();
MyRunListener l = new MyRunListener();
testRunner.addListener(l);
Result r = testRunner.run(request);
Assert.assertEquals(2, r.getRunCount());
Assert.assertEquals(2, l.testCount);
}
@Override
public void testRunFinished(Result result) {
ArrayList<TestSuiteResult> testSuiteResults = new ArrayList<TestSuiteResult>();
logger.info(">>> testRunFinished called");
logger.info("testRunFinished testSuitesMap.size() = " + testSuitesMap.size());
for (Map.Entry<String, TestSuiteResultsStore> testSuiteEntry : testSuitesMap.entrySet()) {
logger.info("testRunFinished testSuiteName = " + testSuiteEntry.getKey());
TestSuiteResultsStore store = testSuiteEntry.getValue();
TestSuiteResult testSuiteResult = new TestSuiteResult(testSuiteEntry.getKey(),
getFormattedDuration(store.consumedTime),
store.tests,
store.errors,
store.skipped,
store.failures,
store.testCaseResults.toArray(new TestCaseResult[store.testCaseResults.size()]));
testSuiteResults.add(testSuiteResult);
}
logger.info("testRunFinished after for loop");
testResult = new TestResult(testSuiteResults.toArray(new TestSuiteResult[testSuiteResults.size()]));
logger.info("<<< testRunFinished called");
}
/**
* Called when all tests have finished. Prints to stdout if the tests were successful or not. If
* not, it also prints the number of failed test cases. Finally, it prints the number of
* ignored test cases.
*
* @param result the summary of the test run, including all the tests that failed
*/
@Override
public void testRunFinished(Result result) throws Exception {
if (result.wasSuccessful()) {
System.out.println("Successfully finished running "
+ formatTestCaseCount(result.getRunCount()) + " in " + result.getRunTime() + " ms.");
} else {
System.out.println("Finished running " + formatTestCaseCount(result.getRunCount())
+ " in " + result.getRunTime() + " ms.");
int failureCount = result.getFailureCount();
if (failureCount == 1) {
System.out.println("There was 1 failed test.");
} else {
System.out.println("There were " + failureCount + " failed tests.");
}
}
int ignoredCount = result.getIgnoreCount();
if (ignoredCount == 1) {
System.out.println(result.getIgnoreCount() + " test case was ignored.");
} else if (ignoredCount > 1) {
System.out.println(result.getIgnoreCount() + " test cases were ignored.");
}
}
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;
}
/** Verify that including and excluding different classes leaves that class's methods. */
@Test
public void testClassAndNotClass_different() {
Request request =
builder
.addTestClass(SampleRunnerFilterSizeTest.class.getName())
.removeTestClass(SampleRunnerFilterClassSize.class.getName())
.build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(2, result.getRunCount());
}
@Override
public void testRunFinished(Result result) throws Exception {
Log.i(
TAG,
String.format(
"run finished: %d tests, %d failed, %d ignored",
result.getRunCount(), result.getFailureCount(), result.getIgnoreCount()));
}
@Test
public void testParameterizedBeforeClassAndAfterClass() throws Exception {
Result result = JUnitCore.runClasses(ParameterizedClassRuleTestClass.class);
assertThat(result.getRunCount(), is(4));
final String expectedLog = "beforeClass pwStarting parameterizedBeforeClass before test after before test after parameterizedAfterClass pwFinished afterClass ";
assertThat(logs[0], is(expectedLog));
assertThat(logs[1], is(expectedLog));
}
/**
* {@link ParameterizedAfterClass}を設定したメソッドのバリデーションテスト
*/
@Test
public void testValidateParameterizedAfterClass() throws Exception {
Result result = JUnitCore.runClasses(ValidateParameterizedAfterClass.class);
assertThat(result.getFailureCount(), is(3));
for (Failure failure : result.getFailures()) {
assertThat(
failure.getMessage(),
is(anyOf(equalTo("Method afterClass() should be static"),
equalTo("Method afterClass() should be public"),
// equalTo("Method afterClass() should have 1 parameters"),
equalTo("Method afterClass() should be void"))));
}
}
/** Test @Suppress when all methods have been filtered */
@Test
public void testSuppress_all() {
Request request =
builder
.addTestClass(SampleAllSuppressed.class.getName())
.addTestClass(SampleJUnit3Suppressed.class.getName())
.build();
JUnitCore testRunner = new JUnitCore();
MyRunListener l = new MyRunListener();
testRunner.addListener(l);
Result r = testRunner.run(request);
Assert.assertEquals(2, r.getRunCount());
Assert.assertEquals(2, l.testCount);
}
public static void resultReport(Result result) {
System.out.println("Finished. Result: Failures: " +
result.getFailureCount() + ". Ignored: " +
result.getIgnoreCount() + ". Tests run: " +
result.getRunCount() + ". Time: " +
result.getRunTime() + "ms.");
}
protected void printFooter(Result result) {
if (result.wasSuccessful()) {
writer.println("\n");
writer.print("OK");
writer.println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")");
} else {
writer.println();
writer.println("\nFAILURES!!!");
writer.println("Tests run: " + result.getRunCount() + ", Failures: " + result.getFailureCount());
}
writer.println();
}
/** Test that a custom RunnerBuilder is used. */
@Test
public void testCustomRunnerBuilder() {
Request request =
builder
.addCustomRunnerBuilderClass(CustomRunnerBuilder.class)
.addTestClass(BrokenRunnableTest.class.getName())
.build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
assertEquals("unexpected run count", 1, result.getRunCount());
assertEquals("unexpected failure count", 1, result.getFailureCount());
Failure failure = result.getFailures().get(0);
assertEquals("unexpected failure", "Broken", failure.getMessage());
}
@Test
public void testNonStaticGeneratorVoidReturn()
{
// test a non static generator
Result result = JUnitCore.runClasses(NonStaticGeneratorVoidReturn.class);
checkFail(result, 1, 0, 1, "java.lang.Exception: No public static parameters method on class " +
NonStaticGeneratorVoidReturn.class.getCanonicalName());
}
@Test
public void testGeneratorTooFewElements()
{
// one test iteration with two parameter fields, but just one data set
Result result = JUnitCore.runClasses(GeneratorTooFewElements.class);
checkFail(result, 1, 0, 1, "Number of parameters (1) and fields (2) annotated with @Parameter must match!");
}
@org.junit.Test
public void testThirdPartyAssertAsyncFailure() {
Result result = run(ThirdPartyAssertAsyncFailure.class);
assertEquals(1, result.getRunCount());
assertEquals(1, result.getFailureCount());
assertEquals("Testing async failure", result.getFailures().get(0).getMessage());
}
@Test
public void testTimeoutsInJUnit4WithRule() {
Request request =
builder
.addTestClass(JUnit4WithRuleClass.class.getName())
.setPerTestTimeout(JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT)
.build();
JUnitCore junitCore = new JUnitCore();
Result result = junitCore.run(request);
assertThat(result.getFailures()).isEmpty();
assertThat(result.getRunCount()).isEqualTo(2);
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JPLTestSuite.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
failure.getException().printStackTrace();
}
System.out.println("********* Test successful? " + result.wasSuccessful());
}
@Test
public void missingTableTest()
{
Class testClass = LifecycleTestMissingTableInnerTest.class;
Result result = runInnerTest(testClass);
Assert.assertFalse(result.wasSuccessful());
Assert.assertTrue(getOutputFile(testClass).exists());
Assert.assertEquals(Arrays.asList("onStarted", "onFailed", "onFinished"), EVENTS.get(testClass));
}
@Test
public void testFailingMethodRule() throws Exception {
Result result = new JUnitCore().run(new VertxUnitRunner(FailingMethodRuleTestSuite.class));
assertEquals(1, FailingMethodRuleTestSuite.rule.failures.size());
assertEquals(1, result.getFailures().size());
Failure failure = result.getFailures().get(0);
assertSame(failure.getException(), FailingMethodRuleTestSuite.rule.failures.get(0));
assertEquals(1, FailingMethodRuleTestSuite.rule.methods.size());
assertEquals(FailingMethodRuleTestSuite.class.getDeclaredMethod("testMethod", TestContext.class), FailingMethodRuleTestSuite.rule.methods.get(0).getMethod());
assertEquals(1, FailingMethodRuleTestSuite.rule.targets.size());
assertTrue(FailingMethodRuleTestSuite.rule.targets.get(0) instanceof FailingMethodRuleTestSuite);
assertEquals(1, FailingMethodRuleTestSuite.rule.evaluateCount.get());
}
/**
* Verify that inclusion filter is filtering out all other tests in the same class and leaves the
* rest of the inclusion filters
*/
@Test
public void testMultipleMethodInclusions() {
Request request =
builder
.addTestClass(SampleTwoTestsClass.class.getName())
.addTestMethod(SampleThreeTestsClass.class.getName(), "test1of3")
.addTestMethod(SampleThreeTestsClass.class.getName(), "test3of3")
.build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(4, result.getRunCount());
}