Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / unittest / gtest_testrunner.py
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Implements a unittest TestRunner with GTest output.
7
8 This output is ported from gtest.cc's PrettyUnitTestResultPrinter, but
9 designed to be a drop-in replacement for unittest's TextTestRunner.
10 """
11
12 import sys
13 import time
14 import unittest
15
16 from telemetry.unittest import gtest_unittest_results
17
18
19 class GTestTestSuite(unittest.TestSuite):
20   def __call__(self, *args, **kwargs):
21     result = args[0]
22     timestamp = time.time()
23     unit = 'test' if len(self._tests) == 1 else 'tests'
24     if not any(isinstance(x, unittest.TestSuite) for x in self._tests):
25       print '[----------] %d %s' % (len(self._tests), unit)
26     for test in self._tests:
27       if result.shouldStop:
28         break
29       test(result)
30     endts = time.time()
31     ms = (endts - timestamp) * 1000
32     if not any(isinstance(x, unittest.TestSuite) for x in self._tests):
33       print '[----------] %d %s (%d ms total)' % (len(self._tests), unit, ms)
34       print
35     return result
36
37
38 class GTestTestRunner(object):
39   def __init__(self, print_result_after_run=True):
40     self.print_result_after_run = print_result_after_run
41     self.result = gtest_unittest_results.GTestUnittestResults(sys.stdout)
42
43   def run(self, test):
44     "Run the given test case or test suite."
45     test(self.result)
46     if self.print_result_after_run:
47       self.result.PrintSummary()
48     return self.result