下面列出了org.junit.runner.Result#wasSuccessful() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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.");
}
}
@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 = findEnd2EndTestClasses();
System.out.println("Found " + classes.length + " end2end tests to run:");
for (Class<?> aClass : classes) {
System.out.println(" " + aClass);
}
if(skipTests) return 0;
JUnitCore junit = new JUnitCore();
junit.addListener(new End2EndTestListenter(System.out));
Result result = junit.run(classes);
return result.wasSuccessful() ? 0 : 1;
}
/**
* 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;
}
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());
}
}
public boolean passesAllTests(Patch patch, List<TestResult> testClasses, NopolProcessor processor) {
logger.info("Applying patch: {}", patch);
String qualifiedName = patch.getRootClassName();
SpoonedClass spoonedClass = spoonedProject.forked(qualifiedName);
processor.setValue(patch.asString());
ClassLoader loader = spoonedClass.processedAndDumpedToClassLoader(processor);
logger.info("Running test suite to check the patch \"{}\" is working", patch.asString());
Result result = TestSuiteExecution.runTestResult(testClasses, loader, nopolContext);
if (result.wasSuccessful()) {
//spoonedClass.generateOutputFile(destinationFolder());
return true;
} else {
logger.info("Failing tests {}", result.getFailures());
}
return false;
}
/**
* 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;
}
@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 = findEnd2EndTestClasses();
System.out.println("Found " + classes.length + " end2end tests to run:");
for (Class<?> aClass : classes) {
System.out.println(" " + aClass);
}
if(skipTests) return 0;
JUnitCore junit = new JUnitCore();
junit.addListener(new End2EndTestListenter(System.out));
Result result = junit.run(classes);
return result.wasSuccessful() ? 0 : 1;
}
/**
* .
* @param args .
* @throws ClassNotFoundException .
*/
public static void main(String... args) throws ClassNotFoundException {
int retCode = 0;
String resultMessage = "SUCCESS";
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
if (!result.wasSuccessful()) {
retCode = 1;
resultMessage = "FAILURE";
}
System.out.println(resultMessage);
System.exit(retCode);
}
private void doGetHelper(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
writer.println("Test invoked at: ");
writer.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z")
.format(Calendar.getInstance().getTime()));
Result result = new JUnitCore().run(Tester.class);
if (result.wasSuccessful()) {
resp.setStatus(200);
writer.println(
String.format(
"PASS! Tests ran %d, tests ignored %d",
result.getRunCount(),
result.getIgnoreCount()));
} else {
resp.setStatus(500);
writer.println(
String.format(
"FAILED! Tests ran %d, tests failed %d, tests ignored %d",
result.getRunCount(),
result.getFailureCount(),
result.getIgnoreCount()));
for (Failure failure : result.getFailures()) {
writer.println("================================");
writer.println(failure.getTestHeader());
Throwable thrown = failure.getException();
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
thrown.printStackTrace(printWriter);
writer.println(stringWriter);
}
}
}
public static void runInMain(Class<?> testClass, String[] args) throws InitializationError, NoTestsRemainException {
JUnitCore core = new JUnitCore();
core.addListener(new TextListener(System.out));
HashemTestRunner suite = new HashemTestRunner(testClass);
if (args.length > 0) {
suite.filter(new NameFilter(args[0]));
}
Result r = core.run(suite);
if (!r.wasSuccessful()) {
System.exit(1);
}
}
/**
* Run Marathon in batch mode.
*/
private static void runBatchMode() {
String projectDir = argProcessor.getProjectDirectory();
if (projectDir == null) {
argProcessor.help("No project directory");
return;
}
if (!ProjectFile.isValidProjectDirectory(new File(projectDir))) {
argProcessor.help("`" + projectDir + "` is an invalid project folder. Please provide a valid Marathon project folder.");
return;
}
if (projectDir.endsWith(".mpf") && new File(projectDir).isFile()) {
argProcessor.help("A marathon project file is given.\nUse project directory instead");
return;
}
processMPF(projectDir, true);
OSUtils.setLogConfiguration(projectDir);
RuntimeLogger.setRuntimeLogger(new NullLogger());
cleanResultFolder();
TestRunner aTestRunner = createTestRunner();
try {
Result r = aTestRunner.runTests(argProcessor);
if (!r.wasSuccessful()) {
System.exit(junit.textui.TestRunner.FAILURE_EXIT);
}
System.exit(junit.textui.TestRunner.SUCCESS_EXIT);
} catch (Exception e) {
LOGGER.severe(e.getMessage());
System.exit(junit.textui.TestRunner.EXCEPTION_EXIT);
}
}
/**
* 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) {
JUnitCore junitCore = new JUnitCore();
junitCore.addListener(listener);
boolean hasError = false;
for (@AutoreleasePool Class<?> c : classes) {
Result result = junitCore.run(c);
hasError = hasError || !result.wasSuccessful();
}
return hasError ? 1 : 0;
}
@Test
public void testLoad_Pass() {
ExecutorServiceRunner executorServiceRunner = new ExecutorServiceRunner("load_config_test.properties");
final AtomicInteger passedCounter = new AtomicInteger();
final AtomicInteger failedCounter = new AtomicInteger();
Runnable taskSampleTest = () -> {
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- Start. Time = " + LocalDateTime.now());
Result result = (new JUnitCore()).run(Request.method(JunitTestSample.class, "testFirstName"));
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- *Finished Time, result = " + LocalDateTime.now() + " -" + result.wasSuccessful());
if(result.wasSuccessful()){
passedCounter.incrementAndGet();
} else {
failedCounter.incrementAndGet();
}
};
executorServiceRunner.addRunnable(taskSampleTest);
executorServiceRunner.runRunnables();
System.out.println(">>> passed count:" + passedCounter.get());
System.out.println(">>> failed count:" + failedCounter.get());
System.out.println(">>> Total test count:" + (failedCounter.get() + passedCounter.get()));
assertThat(failedCounter.get(), is(0));
}
@Test
public void testLoad_Fail() {
ExecutorServiceRunner executorServiceRunner = new ExecutorServiceRunner("load_config_test.properties");
final AtomicInteger passedCounter = new AtomicInteger();
final AtomicInteger failedCounter = new AtomicInteger();
Runnable taskSampleTest = () -> {
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- Start. Time = " + LocalDateTime.now());
Result result = (new JUnitCore()).run(Request.method(JunitTestSample.class, "testFirstName_fail"));
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- *Finished Time, result = " + LocalDateTime.now() + " -" + result.wasSuccessful());
if(result.wasSuccessful()){
passedCounter.incrementAndGet();
} else {
failedCounter.incrementAndGet();
}
};
executorServiceRunner.addRunnable(taskSampleTest);
executorServiceRunner.runRunnables();
System.out.println(">>> passed count:" + passedCounter.get());
System.out.println(">>> failed count:" + failedCounter.get());
System.out.println(">>> Total test count:" + (failedCounter.get() + passedCounter.get()));
assertThat(passedCounter.get(), is(0));
}
@Test
public void testLoad_Pass() {
ExecutorServiceRunner executorServiceRunner = new ExecutorServiceRunner("load_config_test.properties");
final AtomicInteger passedCounter = new AtomicInteger();
final AtomicInteger failedCounter = new AtomicInteger();
Runnable taskSampleTest = () -> {
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- Start. Time = " + LocalDateTime.now());
Result result = (new JUnitCore()).run(Request.method(JunitRestTestSample.class, "testGetCallToHome_pass"));
System.out.println(Thread.currentThread().getName() + " JunitTestSample test- *Finished Time, result = " + LocalDateTime.now() + " -" + result.wasSuccessful());
System.out.println(">> result.getRunTime()" + result.getRunTime());
System.out.println(">> result.getFailureCount()" + result.getFailureCount());
System.out.println(">> result.getFailures()" + result.getFailures());
if(result.wasSuccessful()){
passedCounter.incrementAndGet();
} else {
failedCounter.incrementAndGet();
}
};
executorServiceRunner.addRunnable(taskSampleTest);
executorServiceRunner.runRunnables();
System.out.println(">>> passed count:" + passedCounter.get());
System.out.println(">>> failed count:" + failedCounter.get());
System.out.println(">>> Total test count:" + (failedCounter.get() + passedCounter.get()));
assertThat(failedCounter.get(), is(0));
}
@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;
}
public static Collection<Result> filter(Collection<Result> results, boolean passing) {
Collection<Result> filtered = MetaList.newLinkedList();
for (Result result : results) {
if (result.wasSuccessful() == passing) {
filtered.add(result);
}
}
return filtered;
}
public void run() {
Result result = JUnitCore.runClasses(
EqualsTest.class,
ListUtilTest.class,
MockingProgressImplTest.class,
TimesTest.class,
MockHandlerTest.class,
AllInvocationsFinderTest.class,
ReturnsEmptyValuesTest.class,
NumberOfInvocationsCheckerTest.class,
RegisteredInvocationsTest.class,
MissingInvocationCheckerTest.class,
NumberOfInvocationsInOrderCheckerTest.class,
MissingInvocationInOrderCheckerTest.class,
ClassImposterizerTest.class,
InvocationMatcherTest.class,
InvocationsFinderTest.class,
InvocationTest.class,
MockitoTest.class,
MockUtilTest.class,
ReporterTest.class,
MockitoAssertionErrorTest.class,
MockitoExceptionTest.class,
StackTraceFilteringTest.class,
BridgeMethodPuzzleTest.class,
OverloadingPuzzleTest.class,
InvalidUsageTest.class,
UsingVarargsTest.class,
CustomMatchersTest.class,
ComparableMatchersTest.class,
InvalidUseOfMatchersTest.class,
MatchersTest.class,
MatchersToStringTest.class,
VerificationAndStubbingUsingMatchersTest.class,
BasicStubbingTest.class,
ReturningDefaultValuesTest.class,
StubbingWithThrowablesTest.class,
AtMostXVerificationTest.class,
BasicVerificationTest.class,
ExactNumberOfTimesVerificationTest.class,
VerificationInOrderTest.class,
NoMoreInteractionsVerificationTest.class,
SelectedMocksInOrderVerificationTest.class,
VerificationOnMultipleMocksUsingMatchersTest.class,
VerificationUsingMatchersTest.class,
RelaxedVerificationInOrderTest.class,
DescriptiveMessagesWhenVerificationFailsTest.class,
DescriptiveMessagesWhenTimesXVerificationFailsTest.class,
BasicVerificationInOrderTest.class,
VerificationInOrderMixedWithOrdiraryVerificationTest.class,
DescriptiveMessagesOnVerificationInOrderErrorsTest.class,
InvalidStateDetectionTest.class,
ReplacingObjectMethodsTest.class,
ClickableStackTracesTest.class,
ExampleTest.class,
PointingStackTraceToActualInvocationTest.class,
VerificationInOrderFromMultipleThreadsTest.class,
ResetTest.class
);
if (!result.wasSuccessful()) {
System.err.println("Thread[" + Thread.currentThread().getId() + "]: error!");
List<Failure> failures = result.getFailures();
System.err.println(failures.size());
for (Failure failure : failures) {
System.err.println(failure.getTrace());
failed = true;
}
}
}
public void run() {
File[] seedFiles = readSeedFiles();
if (this.dependent != null) {
if (this.dependent.exitOnCrash) {
System.setProperty("jqf.ei.EXIT_ON_CRASH", "true");
}
if (this.dependent.exactCrashPath != null) {
System.setProperty("jqf.ei.EXACT_CRASH_PATH", this.dependent.exactCrashPath);
}
}
if (this.libFuzzerCompatOutput) {
System.setProperty("jqf.ei.LIBFUZZER_COMPAT_OUTPUT", "true");
}
try {
ClassLoader loader = new InstrumentingClassLoader(
this.testPackageName.split(File.pathSeparator),
ZestCLI.class.getClassLoader());
// Load the guidance
String title = this.testClassName+"#"+this.testMethodName;
ZestGuidance guidance = seedFiles.length > 0 ?
new ZestGuidance(title, duration, this.outputDirectory, seedFiles) :
new ZestGuidance(title, duration, this.outputDirectory);
guidance.setBlind(blindFuzzing);
// Run the Junit test
Result res = GuidedFuzzing.run(testClassName, testMethodName, loader, guidance, System.out);
if (Boolean.getBoolean("jqf.logCoverage")) {
System.out.println(String.format("Covered %d edges.",
guidance.getTotalCoverage().getNonZeroCount()));
}
if (Boolean.getBoolean("jqf.ei.EXIT_ON_CRASH") && !res.wasSuccessful()) {
System.exit(3);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
/**
* Test runner entry point
* @param args list of test classes to run
* @throws ClassNotFoundException can't load the specified classes
*/
@SuppressWarnings("unchecked")
public static void main(final String[] args) throws ClassNotFoundException {
final JUnitCore jUnitCore = new JUnitCore();
if (CAPTURE_OUTPUT) {
jUnitCore.addListener(new OutputListener());
}
jUnitCore.addListener(new TextListener(new RealSystem()));
final TimingListener timing;
if (COLLECT_TIMINGS) {
timing = new TimingListener();
jUnitCore.addListener(timing);
} else {
timing = null;
}
if (PRINT_FAILURES) {
jUnitCore.addListener(new FailureListener());
}
if (PRINT_NAMES) {
jUnitCore.addListener(new NameListener());
}
jUnitCore.addListener(new NewLineListener());
final List<Result> results = new ArrayList<>();
if (args.length > 0) {
for (final String arg : args) {
final Class<?> klass = ClassLoader.getSystemClassLoader().loadClass(arg);
results.add(jUnitCore.run(klass));
}
} else {
final Class<?>[] classes = getClasses();
results.add(jUnitCore.run(classes));
}
if (timing != null) {
final Map<String, Long> timings = timing.getTimings(LONG_TEST_THRESHOLD);
if (timings.size() > 1) {
System.out.println();
System.out.println("Long tests");
for (Map.Entry<String, Long> entry : timings.entrySet()) {
System.out.println(formatTimeRow(entry.getKey(), entry.getValue(), TIMING_WIDTH));
}
}
}
for (Result result : results) {
if (!result.wasSuccessful()) {
System.exit(1);
}
}
}