1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.result; 17 18 import com.android.ddmlib.Log; 19 import com.android.ddmlib.Log.LogLevel; 20 import com.android.ddmlib.testrunner.TestResult.TestStatus; 21 import com.android.tradefed.config.Option; 22 import com.android.tradefed.config.OptionClass; 23 24 import java.util.ArrayList; 25 import java.util.Collections; 26 import java.util.LinkedList; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Result reporter to print the test results to the console. 32 * <p> 33 * Prints each test run, each test case, and test metrics, test logs, and test file locations. 34 * <p> 35 */ 36 @OptionClass(alias = "console-result-reporter") 37 public class ConsoleResultReporter extends CollectingTestListener implements ILogSaverListener { 38 private static final String LOG_TAG = ConsoleResultReporter.class.getSimpleName(); 39 40 @Option(name = "suppress-passed-tests", description = "For functional tests, ommit summary for " 41 + "passing tests, only print failed and ignored ones") 42 private boolean mSuppressPassedTest = false; 43 44 private List<LogFile> mLogFiles = new LinkedList<>(); 45 46 /** 47 * {@inheritDoc} 48 */ 49 @Override invocationEnded(long elapsedTime)50 public void invocationEnded(long elapsedTime) { 51 Log.logAndDisplay(LogLevel.INFO, LOG_TAG, getInvocationSummary()); 52 } 53 54 /** 55 * {@inheritDoc} 56 */ 57 @Override testLogSaved(String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile)58 public void testLogSaved(String dataName, LogDataType dataType, InputStreamSource dataStream, 59 LogFile logFile) { 60 mLogFiles.add(logFile); 61 } 62 63 /** 64 * {@inheritDoc} 65 */ 66 @Override setLogSaver(ILogSaver logSaver)67 public void setLogSaver(ILogSaver logSaver) { 68 // Ignore. This class doesn't save any additional files. 69 } 70 71 /** 72 * Get the invocation summary as a string. 73 */ getInvocationSummary()74 String getInvocationSummary() { 75 if (getRunResults().isEmpty() && mLogFiles.isEmpty()) { 76 return "No test results\n"; 77 } 78 79 StringBuilder sb = new StringBuilder(); 80 for (TestRunResult testRunResult : getRunResults()) { 81 sb.append(getTestRunSummary(testRunResult)); 82 } 83 if (!mLogFiles.isEmpty()) { 84 sb.append("Log Files:\n"); 85 for (LogFile logFile : mLogFiles) { 86 final String url = logFile.getUrl(); 87 sb.append(String.format(" %s\n", url != null ? url : logFile.getPath())); 88 } 89 } 90 return "Test results:\n" + sb.toString().trim() + "\n"; 91 } 92 93 /** 94 * Get the test run summary as a string including run metrics. 95 */ getTestRunSummary(TestRunResult testRunResult)96 String getTestRunSummary(TestRunResult testRunResult) { 97 StringBuilder sb = new StringBuilder(); 98 sb.append(String.format("%s:", testRunResult.getName())); 99 if (testRunResult.getNumTests() > 0) { 100 sb.append(String.format(" %d Test%s, %d Passed, %d Failed, %d Ignored", 101 testRunResult.getNumCompleteTests(), 102 testRunResult.getNumCompleteTests() == 1 ? "" : "s", // Pluralize Test 103 testRunResult.getNumTestsInState(TestStatus.PASSED), 104 testRunResult.getNumAllFailedTests(), 105 testRunResult.getNumTestsInState(TestStatus.IGNORED))); 106 } else if (testRunResult.getRunMetrics().size() == 0) { 107 sb.append(" No results"); 108 } 109 sb.append("\n"); 110 Map<TestDescription, TestResult> testResults = testRunResult.getTestResults(); 111 for (Map.Entry<TestDescription, TestResult> entry : testResults.entrySet()) { 112 if (mSuppressPassedTest && TestStatus.PASSED.equals(entry.getValue().getStatus())) { 113 continue; 114 } 115 sb.append(getTestSummary(entry.getKey(), entry.getValue())); 116 } 117 Map<String, String> metrics = testRunResult.getRunMetrics(); 118 if (metrics != null && !metrics.isEmpty()) { 119 List<String> metricKeys = new ArrayList<String>(metrics.keySet()); 120 Collections.sort(metricKeys); 121 for (String metricKey : metricKeys) { 122 sb.append(String.format(" %s: %s\n", metricKey, metrics.get(metricKey))); 123 } 124 } 125 sb.append("\n"); 126 return sb.toString(); 127 } 128 129 /** 130 * Get the test summary as string including test metrics. 131 */ getTestSummary(TestDescription testId, TestResult testResult)132 String getTestSummary(TestDescription testId, TestResult testResult) { 133 StringBuilder sb = new StringBuilder(); 134 sb.append(String.format(" %s: %s (%dms)\n", testId.toString(), testResult.getStatus(), 135 testResult.getEndTime() - testResult.getStartTime())); 136 String stack = testResult.getStackTrace(); 137 if (stack != null && !stack.isEmpty()) { 138 sb.append(" stack=\n"); 139 String lines[] = stack.split("\\r?\\n"); 140 for (String line : lines) { 141 sb.append(String.format(" %s\n", line)); 142 } 143 } 144 Map<String, String> metrics = testResult.getMetrics(); 145 if (metrics != null && !metrics.isEmpty()) { 146 List<String> metricKeys = new ArrayList<String>(metrics.keySet()); 147 Collections.sort(metricKeys); 148 for (String metricKey : metricKeys) { 149 sb.append(String.format(" %s: %s\n", metricKey, metrics.get(metricKey))); 150 } 151 } 152 153 return sb.toString(); 154 } 155 } 156