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 * Get the invocation summary as a string. 65 */ getInvocationSummary()66 String getInvocationSummary() { 67 if (getMergedTestRunResults().isEmpty() && mLogFiles.isEmpty()) { 68 return "No test results\n"; 69 } 70 71 StringBuilder sb = new StringBuilder(); 72 for (TestRunResult testRunResult : getMergedTestRunResults()) { 73 sb.append(getTestRunSummary(testRunResult)); 74 } 75 if (!mLogFiles.isEmpty()) { 76 sb.append("Log Files:\n"); 77 for (LogFile logFile : mLogFiles) { 78 final String url = logFile.getUrl(); 79 sb.append(String.format(" %s\n", url != null ? url : logFile.getPath())); 80 } 81 } 82 return "Test results:\n" + sb.toString().trim() + "\n"; 83 } 84 85 /** 86 * Get the test run summary as a string including run metrics. 87 */ getTestRunSummary(TestRunResult testRunResult)88 String getTestRunSummary(TestRunResult testRunResult) { 89 StringBuilder sb = new StringBuilder(); 90 sb.append(String.format("%s:", testRunResult.getName())); 91 if (testRunResult.getNumTests() > 0) { 92 sb.append(String.format(" %d Test%s, %d Passed, %d Failed, %d Ignored", 93 testRunResult.getNumCompleteTests(), 94 testRunResult.getNumCompleteTests() == 1 ? "" : "s", // Pluralize Test 95 testRunResult.getNumTestsInState(TestStatus.PASSED), 96 testRunResult.getNumAllFailedTests(), 97 testRunResult.getNumTestsInState(TestStatus.IGNORED))); 98 } else if (testRunResult.getRunMetrics().size() == 0) { 99 sb.append(" No results"); 100 } 101 sb.append("\n"); 102 Map<TestDescription, TestResult> testResults = testRunResult.getTestResults(); 103 for (Map.Entry<TestDescription, TestResult> entry : testResults.entrySet()) { 104 if (mSuppressPassedTest && TestStatus.PASSED.equals(entry.getValue().getStatus())) { 105 continue; 106 } 107 sb.append(getTestSummary(entry.getKey(), entry.getValue())); 108 } 109 Map<String, String> metrics = testRunResult.getRunMetrics(); 110 if (metrics != null && !metrics.isEmpty()) { 111 List<String> metricKeys = new ArrayList<String>(metrics.keySet()); 112 Collections.sort(metricKeys); 113 for (String metricKey : metricKeys) { 114 sb.append(String.format(" %s: %s\n", metricKey, metrics.get(metricKey))); 115 } 116 } 117 sb.append("\n"); 118 return sb.toString(); 119 } 120 121 /** 122 * Get the test summary as string including test metrics. 123 */ getTestSummary(TestDescription testId, TestResult testResult)124 String getTestSummary(TestDescription testId, TestResult testResult) { 125 StringBuilder sb = new StringBuilder(); 126 sb.append(String.format(" %s: %s (%dms)\n", testId.toString(), testResult.getStatus(), 127 testResult.getEndTime() - testResult.getStartTime())); 128 String stack = testResult.getStackTrace(); 129 if (stack != null && !stack.isEmpty()) { 130 sb.append(" stack=\n"); 131 String lines[] = stack.split("\\r?\\n"); 132 for (String line : lines) { 133 sb.append(String.format(" %s\n", line)); 134 } 135 } 136 Map<String, String> metrics = testResult.getMetrics(); 137 if (metrics != null && !metrics.isEmpty()) { 138 List<String> metricKeys = new ArrayList<String>(metrics.keySet()); 139 Collections.sort(metricKeys); 140 for (String metricKey : metricKeys) { 141 sb.append(String.format(" %s: %s\n", metricKey, metrics.get(metricKey))); 142 } 143 } 144 145 return sb.toString(); 146 } 147 } 148