1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.testing.local;
7 import org.junit.runner.Description;
8 import org.junit.runner.notification.Failure;
10 import java.io.PrintStream;
14 * Formats and logs test status information in googletest-style.
16 public class GtestLogger {
18 private final PrintStream mOutputStream;
20 public GtestLogger(PrintStream outputStream) {
21 mOutputStream = outputStream;
25 * Logs the start of an individual test.
27 public void testStarted(Description test) {
28 mOutputStream.format("[ RUN ] %s.%s", test.getClassName(), test.getMethodName());
29 mOutputStream.println();
33 * Logs a test failure.
35 public void testFailed(Failure f) {
36 if (f.getException() != null) {
37 f.getException().printStackTrace(mOutputStream);
42 * Logs the end of an individual test.
44 public void testFinished(Description test, boolean passed, long elapsedTimeMillis) {
46 mOutputStream.format("[ OK ] %s.%s (%d ms)",
47 test.getClassName(), test.getMethodName(), elapsedTimeMillis);
49 mOutputStream.format("[ FAILED ] %s.%s (%d ms)",
50 test.getClassName(), test.getMethodName(), elapsedTimeMillis);
52 mOutputStream.println();
56 * Logs the start of a test case.
58 public void testCaseStarted(Description test, int testCount) {
59 mOutputStream.format("[----------] Run %d test cases from %s", testCount,
61 mOutputStream.println();
65 * Logs the end of a test case.
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();
76 * Logs the start of a test run.
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();
86 * Logs the end of a test run.
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();
104 mOutputStream.println();