• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** Formats and logs test status information in googletest-style. */
14 public class GtestLogger {
15 
16     private final PrintStream mOutputStream;
17 
GtestLogger(PrintStream outputStream)18     public GtestLogger(PrintStream outputStream) {
19         mOutputStream = outputStream;
20     }
21 
22     /** Logs the start of an individual test. */
testStarted(Description test)23     public void testStarted(Description test) {
24         mOutputStream.format("[ RUN      ] %s.%s", test.getClassName(), test.getMethodName());
25         mOutputStream.println();
26     }
27 
28     /** Logs a test failure. */
testFailed(Failure f)29     public void testFailed(Failure f) {
30         if (f.getException() != null) {
31             f.getException().printStackTrace(mOutputStream);
32         }
33     }
34 
35     /** Logs the end of an individual test. */
testFinished(Description test, boolean passed, long elapsedTimeMillis)36     public void testFinished(Description test, boolean passed, long elapsedTimeMillis) {
37         if (passed) {
38             mOutputStream.format(
39                     "[       OK ] %s.%s (%d ms)",
40                     test.getClassName(), test.getMethodName(), elapsedTimeMillis);
41         } else {
42             mOutputStream.format(
43                     "[   FAILED ] %s.%s (%d ms)",
44                     test.getClassName(), test.getMethodName(), elapsedTimeMillis);
45         }
46         mOutputStream.println();
47     }
48 
49     /** Logs the start of a test case. */
testCaseStarted(Description test, int testCount)50     public void testCaseStarted(Description test, int testCount) {
51         mOutputStream.format(
52                 "[----------] Run %d test cases from %s", testCount, test.getClassName());
53         mOutputStream.println();
54     }
55 
56     /** Logs the end of a test case. */
testCaseFinished(Description test, int testCount, long elapsedTimeMillis)57     public void testCaseFinished(Description test, int testCount, long elapsedTimeMillis) {
58         mOutputStream.format(
59                 "[----------] Run %d test cases from %s (%d ms)",
60                 testCount, test.getClassName(), elapsedTimeMillis);
61         mOutputStream.println();
62         mOutputStream.println();
63     }
64 
65     /** Logs the start of a test run. */
testRunStarted(int testCount)66     public void testRunStarted(int testCount) {
67         mOutputStream.format("[==========] Running %d tests.", testCount);
68         mOutputStream.println();
69         mOutputStream.println("[----------] Global test environment set-up.");
70         mOutputStream.println();
71     }
72 
73     /** Logs the end of a test run. */
testRunFinished( int passedTestCount, Set<Description> failedTests, long elapsedTimeMillis)74     public void testRunFinished(
75             int passedTestCount, Set<Description> failedTests, long elapsedTimeMillis) {
76         int totalTestCount = passedTestCount + failedTests.size();
77         mOutputStream.println("[----------] Global test environment tear-down.");
78         mOutputStream.format(
79                 "[==========] %d tests ran. (%d ms total)", totalTestCount, elapsedTimeMillis);
80         mOutputStream.println();
81         mOutputStream.format("[  PASSED  ] %d tests.", passedTestCount);
82         mOutputStream.println();
83         if (!failedTests.isEmpty()) {
84             mOutputStream.format("[  FAILED  ] %d tests.", failedTests.size());
85             mOutputStream.println();
86             for (Description d : failedTests) {
87                 mOutputStream.format("[  FAILED  ] %s.%s", d.getClassName(), d.getMethodName());
88                 mOutputStream.println();
89             }
90             mOutputStream.println();
91         }
92     }
93 }
94