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.Assert;
9 import org.junit.runner.Description;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.BlockJUnit4ClassRunner;
13 import java.io.ByteArrayOutputStream;
14 import java.io.PrintStream;
15 import java.io.Serializable;
16 import java.util.Comparator;
17 import java.util.HashSet;
19 import java.util.TreeSet;
22 * Unit tests for GtestLogger.
24 @RunWith(BlockJUnit4ClassRunner.class)
25 public class GtestLoggerTest {
28 public void testTestStarted() {
29 ByteArrayOutputStream actual = new ByteArrayOutputStream();
30 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
31 loggerUnderTest.testStarted(
32 Description.createTestDescription(GtestLoggerTest.class, "testTestStarted"));
34 "[ RUN ] org.chromium.testing.local.GtestLoggerTest.testTestStarted\n",
39 public void testTestFinishedPassed() {
40 ByteArrayOutputStream actual = new ByteArrayOutputStream();
41 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
42 loggerUnderTest.testFinished(
43 Description.createTestDescription(GtestLoggerTest.class, "testTestFinishedPassed"),
46 "[ OK ] org.chromium.testing.local.GtestLoggerTest.testTestFinishedPassed" +
52 public void testTestFinishedFailed() {
53 ByteArrayOutputStream actual = new ByteArrayOutputStream();
54 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
55 loggerUnderTest.testFinished(
56 Description.createTestDescription(GtestLoggerTest.class, "testTestFinishedPassed"),
59 "[ FAILED ] org.chromium.testing.local.GtestLoggerTest.testTestFinishedPassed" +
65 public void testTestCaseStarted() {
66 ByteArrayOutputStream actual = new ByteArrayOutputStream();
67 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
68 loggerUnderTest.testCaseStarted(
69 Description.createSuiteDescription(GtestLoggerTest.class), 456);
71 "[----------] Run 456 test cases from org.chromium.testing.local.GtestLoggerTest\n",
76 public void testTestCaseFinished() {
77 ByteArrayOutputStream actual = new ByteArrayOutputStream();
78 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
79 loggerUnderTest.testCaseFinished(
80 Description.createSuiteDescription(GtestLoggerTest.class), 456, 123);
82 "[----------] Run 456 test cases from org.chromium.testing.local.GtestLoggerTest" +
88 public void testTestRunStarted() {
89 ByteArrayOutputStream actual = new ByteArrayOutputStream();
90 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
91 loggerUnderTest.testRunStarted(1234);
93 "[==========] Running 1234 tests.\n" +
94 "[----------] Global test environment set-up.\n\n",
99 public void testTestRunFinishedNoFailures() {
100 ByteArrayOutputStream actual = new ByteArrayOutputStream();
101 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
102 loggerUnderTest.testRunFinished(1234, new HashSet<Description>(), 4321);
104 "[----------] Global test environment tear-down.\n" +
105 "[==========] 1234 tests ran. (4321 ms total)\n" +
106 "[ PASSED ] 1234 tests.\n",
111 public void testTestRunFinishedWithFailures() {
112 ByteArrayOutputStream actual = new ByteArrayOutputStream();
113 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
115 Set<Description> failures = new TreeSet<Description>(new DescriptionComparator());
116 failures.add(Description.createTestDescription(
117 "GtestLoggerTest", "testTestRunFinishedNoFailures"));
118 failures.add(Description.createTestDescription(
119 "GtestLoggerTest", "testTestRunFinishedWithFailures"));
121 loggerUnderTest.testRunFinished(1232, failures, 4312);
123 "[----------] Global test environment tear-down.\n" +
124 "[==========] 1234 tests ran. (4312 ms total)\n" +
125 "[ PASSED ] 1232 tests.\n" +
126 "[ FAILED ] 2 tests.\n" +
127 "[ FAILED ] GtestLoggerTest.testTestRunFinishedNoFailures\n" +
128 "[ FAILED ] GtestLoggerTest.testTestRunFinishedWithFailures\n" +
133 private static class DescriptionComparator implements Comparator<Description>, Serializable {
135 public int compare(Description o1, Description o2) {
136 return toGtestStyleString(o1).compareTo(toGtestStyleString(o2));
139 private static String toGtestStyleString(Description d) {
140 return d.getClassName() + "." + d.getMethodName();