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.
4
5import unittest
6
7from telemetry.testing import progress_reporter
8
9
10class TestFoo(unittest.TestCase):
11  # Test method doesn't have test- prefix intentionally. This is so that
12  # run_test script won't run this test.
13  def RunPassingTest(self):
14    pass
15
16  def RunFailingTest(self):
17    self.fail('expected failure')
18
19
20class LoggingProgressReporter(object):
21  def __init__(self):
22    self._call_log = []
23
24  @property
25  def call_log(self):
26    return tuple(self._call_log)
27
28  def __getattr__(self, name):
29    def wrapper(*_):
30      self._call_log.append(name)
31    return wrapper
32
33
34class ProgressReporterTest(unittest.TestCase):
35  def testTestRunner(self):
36    suite = progress_reporter.TestSuite()
37    suite.addTest(TestFoo(methodName='RunPassingTest'))
38    suite.addTest(TestFoo(methodName='RunFailingTest'))
39
40    reporter = LoggingProgressReporter()
41    runner = progress_reporter.TestRunner()
42    progress_reporters = (reporter,)
43    result = runner.run(suite, progress_reporters, 1, None)
44
45    self.assertEqual(len(result.successes), 1)
46    self.assertEqual(len(result.failures), 1)
47    self.assertEqual(len(result.failures_and_errors), 1)
48    expected = (
49        'StartTestRun', 'StartTestSuite',
50        'StartTest', 'Success', 'StopTest',
51        'StartTest', 'Failure', 'StopTest',
52        'StopTestSuite', 'StopTestRun',
53    )
54    self.assertEqual(reporter.call_log, expected)
55