下面列出了junit.framework.TestResult#addListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void runTestSuite( String testClassName ) {
Test suite = getTest( testClassName );
if (suite != null) {
TestResult testResult = new TestResult();
testResult.addListener( this );
long startTime= System.currentTimeMillis();
suite.run( testResult );
long endTime= System.currentTimeMillis();
_formatter.displayResults( _writer, testClassName, elapsedTimeAsString( endTime-startTime ), testResult );
}
}
protected void setUp() {
fResult= new TestResult();
fResult.addListener(this);
fStartCount= 0;
fEndCount= 0;
fFailureCount= 0;
}
public boolean start() throws ContainerException {
// configure log4j output logging
if (logLevel != null) {
int llevel = Debug.getLevelFromString(logLevel);
for (int v = 0; v < 9; v++) {
if (v < llevel) {
Debug.set(v, false);
} else {
Debug.set(v, true);
}
}
}
// get the tests to run
JunitSuiteWrapper jsWrapper = new JunitSuiteWrapper(component, suiteName, testCase);
if (jsWrapper.getAllTestList().size() == 0) {
throw new ContainerException("No tests found (" + component + " / " + suiteName + " / " + testCase + ")");
}
boolean failedRun = false;
for (ModelTestSuite modelSuite: jsWrapper.getModelTestSuites()) {
Delegator testDelegator = modelSuite.getDelegator();
TestSuite suite = modelSuite.makeTestSuite();
JUnitTest test = new JUnitTest();
test.setName(suite.getName());
// create the XML logger
JunitXmlListener xml;
try {
xml = new JunitXmlListener(new FileOutputStream(logDir + suite.getName() + ".xml"));
} catch (FileNotFoundException e) {
throw new ContainerException(e);
}
// per-suite results
TestResult results = new TestResult();
results.addListener(new JunitListener());
results.addListener(xml);
// add the suite to the xml listener
xml.startTestSuite(test);
// run the tests
suite.run(results);
test.setCounts(results.runCount(), results.failureCount(), results.errorCount());
// rollback all entity operations performed by the delegator
testDelegator.rollback();
xml.endTestSuite(test);
if (!results.wasSuccessful()) {
failedRun = true;
}
// display the results
Debug.logInfo("[JUNIT] Results for test suite: " + suite.getName(), module);
Debug.logInfo("[JUNIT] Pass: " + results.wasSuccessful() + " | # Tests: " + results.runCount() + " | # Failed: " +
results.failureCount() + " # Errors: " + results.errorCount(), module);
if (Debug.importantOn() && !results.wasSuccessful()) {
Debug.logInfo("[JUNIT] ----------------------------- ERRORS ----------------------------- [JUNIT]", module);
Enumeration<?> err = results.errors();
if (!err.hasMoreElements()) {
Debug.logInfo("None", module);
} else {
while (err.hasMoreElements()) {
Object error = err.nextElement();
Debug.logInfo("--> " + error, module);
if (error instanceof TestFailure) {
Debug.logInfo(((TestFailure) error).trace(), module);
}
}
}
Debug.logInfo("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
Debug.logInfo("[JUNIT] ---------------------------- FAILURES ---------------------------- [JUNIT]", module);
Enumeration<?> fail = results.failures();
if (!fail.hasMoreElements()) {
Debug.logInfo("None", module);
} else {
while (fail.hasMoreElements()) {
Object failure = fail.nextElement();
Debug.logInfo("--> " + failure, module);
if (failure instanceof TestFailure) {
Debug.logInfo(((TestFailure) failure).trace(), module);
}
}
}
Debug.logInfo("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
}
}
if (failedRun) {
throw new ContainerException("Test run was unsuccessful");
}
return true;
}
@Override
public void run(RunNotifier notifier) {
TestResult result = new TestResult();
result.addListener(createAdaptingListener(notifier));
getTest().run(result);
}
/**
* Called after all test classes are in place, ready to test
*/
protected void start() {
TestCaseCollector collector = getTestCaseCollector(this.getClass().getClassLoader());
try {
collector.addTestClasses(mTestClasses);
} catch (ClassNotFoundException e) {
// will be caught by uncaught handler
throw new RuntimeException(e.getMessage(), e);
}
if (mDebug) {
Debug.waitForDebugger();
}
mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
mHandlerThread.setDaemon(true);
mHandlerThread.start();
UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
automationWrapper.connect();
long startTime = SystemClock.uptimeMillis();
TestResult testRunResult = new TestResult();
ResultReporter resultPrinter;
String outputFormat = mParams.getString("outputFormat");
List<TestCase> testCases = collector.getTestCases();
Bundle testRunOutput = new Bundle();
if ("simple".equals(outputFormat)) {
resultPrinter = new SimpleResultPrinter(System.out, true);
} else {
resultPrinter = new WatcherResultPrinter(testCases.size());
}
try {
automationWrapper.setRunAsMonkey(mMonkey);
mUiDevice = MyUiDevice.getInstance();
UiAutomation uiAutomation = automationWrapper.getUiAutomation();
mUiDevice.initialize(new ShellUiAutomatorBridge(uiAutomation));
mUiDevice.setUiAutomation(uiAutomation);
String traceType = mParams.getString("traceOutputMode");
if (traceType != null) {
Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
String filename = mParams.getString("traceLogFilename");
if (filename == null) {
throw new RuntimeException("Name of log file not specified. " + "Please specify it using traceLogFilename parameter");
}
Tracer.getInstance().setOutputFilename(filename);
}
Tracer.getInstance().setOutputMode(mode);
}
// add test listeners
testRunResult.addListener(resultPrinter);
// add all custom listeners
for (TestListener listener : mTestListeners) {
testRunResult.addListener(listener);
}
// run tests for realz!
for (TestCase testCase : testCases) {
prepareTestCase(testCase);
testCase.run(testRunResult);
}
} catch (Throwable t) {
// catch all exceptions so a more verbose error message can be outputted
resultPrinter.printUnexpectedError(t);
} finally {
long runTime = SystemClock.uptimeMillis() - startTime;
resultPrinter.print(testRunResult, runTime, testRunOutput);
automationWrapper.disconnect();
automationWrapper.setRunAsMonkey(false);
mHandlerThread.quit();
}
}