1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.testing.local; 6 7 import org.junit.runner.Description; 8 import org.junit.runner.notification.Failure; 9 10 import java.io.PrintStream; 11 import java.util.Set; 12 13 /** 14 * Formats and logs test status information in googletest-style. 15 */ 16 public class GtestLogger { 17 18 private final PrintStream mOutputStream; 19 GtestLogger(PrintStream outputStream)20 public GtestLogger(PrintStream outputStream) { 21 mOutputStream = outputStream; 22 } 23 24 /** 25 * Logs the start of an individual test. 26 */ testStarted(Description test)27 public void testStarted(Description test) { 28 mOutputStream.format("[ RUN ] %s.%s", test.getClassName(), test.getMethodName()); 29 mOutputStream.println(); 30 } 31 32 /** 33 * Logs a test failure. 34 */ testFailed(Failure f)35 public void testFailed(Failure f) { 36 if (f.getException() != null) { 37 f.getException().printStackTrace(mOutputStream); 38 } 39 } 40 41 /** 42 * Logs the end of an individual test. 43 */ testFinished(Description test, boolean passed, long elapsedTimeMillis)44 public void testFinished(Description test, boolean passed, long elapsedTimeMillis) { 45 if (passed) { 46 mOutputStream.format("[ OK ] %s.%s (%d ms)", 47 test.getClassName(), test.getMethodName(), elapsedTimeMillis); 48 } else { 49 mOutputStream.format("[ FAILED ] %s.%s (%d ms)", 50 test.getClassName(), test.getMethodName(), elapsedTimeMillis); 51 } 52 mOutputStream.println(); 53 } 54 55 /** 56 * Logs the start of a test case. 57 */ testCaseStarted(Description test, int testCount)58 public void testCaseStarted(Description test, int testCount) { 59 mOutputStream.format("[----------] Run %d test cases from %s", testCount, 60 test.getClassName()); 61 mOutputStream.println(); 62 } 63 64 /** 65 * Logs the end of a test case. 66 */ testCaseFinished(Description test, int testCount, long elapsedTimeMillis)67 public void testCaseFinished(Description test, int testCount, 68 long elapsedTimeMillis) { 69 mOutputStream.format("[----------] Run %d test cases from %s (%d ms)", 70 testCount, test.getClassName(), elapsedTimeMillis); 71 mOutputStream.println(); 72 mOutputStream.println(); 73 } 74 75 /** 76 * Logs the start of a test run. 77 */ testRunStarted(int testCount)78 public void testRunStarted(int testCount) { 79 mOutputStream.format("[==========] Running %d tests.", testCount); 80 mOutputStream.println(); 81 mOutputStream.println("[----------] Global test environment set-up."); 82 mOutputStream.println(); 83 } 84 85 /** 86 * Logs the end of a test run. 87 */ testRunFinished(int passedTestCount, Set<Description> failedTests, long elapsedTimeMillis)88 public void testRunFinished(int passedTestCount, Set<Description> failedTests, 89 long elapsedTimeMillis) { 90 int totalTestCount = passedTestCount + failedTests.size(); 91 mOutputStream.println("[----------] Global test environment tear-down."); 92 mOutputStream.format("[==========] %d tests ran. (%d ms total)", 93 totalTestCount, elapsedTimeMillis); 94 mOutputStream.println(); 95 mOutputStream.format("[ PASSED ] %d tests.", passedTestCount); 96 mOutputStream.println(); 97 if (!failedTests.isEmpty()) { 98 mOutputStream.format("[ FAILED ] %d tests.", failedTests.size()); 99 mOutputStream.println(); 100 for (Description d : failedTests) { 101 mOutputStream.format("[ FAILED ] %s.%s", d.getClassName(), d.getMethodName()); 102 mOutputStream.println(); 103 } 104 mOutputStream.println(); 105 } 106 } 107 108 } 109 110