aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/javatests/com/google/testing/junit/runner/TestListener.java
blob: 2573574094a1f9df5e649342ecce5bf80d587e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.runner;

import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * A straightforward listener that prints to stdout/stderr whenever a test changes its state (e.g.
 * started, finished, failed).
 */
class TestListener extends RunListener {
    /**
     * Called before any tests have been run. Prints to stdout the number of test cases found.
     *
     * @param description describes the tests to be run
     */
    @Override
    public void testRunStarted(Description description) throws Exception {
      System.out.println("Found " + formatTestCaseCount(description.testCount()) + ".");
    }

    /**
     * Called when all tests have finished. Prints to stdout if the tests were successful or not. If
     * not, it also prints the number of failed test cases. Finally, it prints the number of
     * ignored test cases.
     *
     * @param result the summary of the test run, including all the tests that failed
     */
    @Override
    public void testRunFinished(Result result) throws Exception {
      if (result.wasSuccessful()) {
        System.out.println("Successfully finished running "
            + formatTestCaseCount(result.getRunCount()) + " in " + result.getRunTime() + " ms.");
      } else {
        System.out.println("Finished running " + formatTestCaseCount(result.getRunCount())
            + " in " + result.getRunTime() + " ms.");
        int failureCount = result.getFailureCount();
        if (failureCount == 1) {
          System.out.println("There was 1 failed test.");
        } else {
          System.out.println("There were " + failureCount + " failed tests.");
        }
      }
      int ignoredCount = result.getIgnoreCount();
      if (ignoredCount == 1) {
        System.out.println(result.getIgnoreCount() + " test case was ignored.");
      } else if (ignoredCount > 1) {
        System.out.println(result.getIgnoreCount() + " test cases were ignored.");
      }
    }

    /**
     * Called when an atomic test is about to be started. Prints to stdout the name of the test that
     * started with the corresponding information.
     *
     * @param description the description of the test that is about to be run
     * (generally a class and method name)
     */
    @Override
    public void testStarted(Description description) throws Exception {
      System.out.println("Test case started: " + description.getDisplayName());
    }

    /**
     * Called when an atomic test fails. Prints to stderr the name of the test that failed
     * (including its class) and the reason why, including the stack trace.
     *
     * @param failure describes the test that failed and the exception that was thrown
     */
    @Override
    public void testFailure(Failure failure) throws Exception {
      System.err.println("Failure in " + failure.getTestHeader() + ": " + failure.getMessage()
          + "\n" + failure.getTrace());
    }

    /**
     * Called when an atomic test flags that it assumes a condition that is false. Prints to stderr
     * that a test case assumed false condition, including the corresponding message containing
     * the context.
     *
     * @param failure describes the test that failed and the
     * {@link AssumptionViolatedException} that was thrown
     */
    @Override
    public void testAssumptionFailure(Failure failure) {
      System.err.println("Test case assumed false condition: " + failure.getMessage());
    }

    /**
     *  Called when a test will not be run, generally because a test method is annotated with
     *  Ignore. Prints to stderr that a test case was ignored, alongside with the test name.
     **/
    @Override
    public void testIgnored(Description description) throws java.lang.Exception {
      System.err.println("Test case " + description.getMethodName() + " ignored.");
    }

    private static String formatTestCaseCount(int count) {
      if (count == 1) {
        return "1 test case";
      }
      return count + " test cases";
    }
  }