blob: 096039915b12cc03c0737034cc4601a05d6db054 [file] [log] [blame]
commit-bot@chromium.org11f15622014-01-07 17:03:40 +00001#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9A wrapper around the standard Python unittest library, adding features we need
10for various unittests within this directory.
11"""
12
13import os
14import subprocess
15import unittest
16
17
18class TestCase(unittest.TestCase):
19
20 def shortDescription(self):
21 """Tell unittest framework to not print docstrings for test cases."""
22 return None
23
24 def run_command(self, args):
25 """Runs a program from the command line and returns stdout.
26
27 Args:
28 args: Command line to run, as a list of string parameters. args[0] is the
29 binary to run.
30
31 Returns:
32 stdout from the program, as a single string.
33
34 Raises:
35 Exception: the program exited with a nonzero return code.
36 """
37 proc = subprocess.Popen(args,
38 stdout=subprocess.PIPE,
39 stderr=subprocess.PIPE)
40 (stdout, stderr) = proc.communicate()
41 if proc.returncode is not 0:
42 raise Exception('command "%s" failed: %s' % (args, stderr))
43 return stdout
44
45 def find_path_to_program(self, program):
46 """Returns path to an existing program binary.
47
48 Args:
49 program: Basename of the program to find (e.g., 'render_pictures').
50
51 Returns:
52 Absolute path to the program binary, as a string.
53
54 Raises:
55 Exception: unable to find the program binary.
56 """
57 trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
58 os.pardir, os.pardir))
59 possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
60 os.path.join(trunk_path, 'out', 'Debug', program),
61 os.path.join(trunk_path, 'out', 'Release',
62 program + '.exe'),
63 os.path.join(trunk_path, 'out', 'Debug',
64 program + '.exe')]
65 for try_path in possible_paths:
66 if os.path.isfile(try_path):
67 return try_path
68 raise Exception('cannot find %s in paths %s; maybe you need to '
69 'build %s?' % (program, possible_paths, program))
70
71
72def main(test_case_class):
73 """Run the unit tests within the given class.
74
75 Raises an Exception if any of those tests fail (in case we are running in the
76 context of run_all.py, which depends on that Exception to signal failures).
77
78 TODO(epoger): Make all of our unit tests use the Python unittest framework,
79 so we can leverage its ability to run *all* the tests and report failures at
80 the end.
81 """
82 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class)
83 results = unittest.TextTestRunner(verbosity=2).run(suite)
84 if not results.wasSuccessful():
85 raise Exception('failed unittest %s' % test_case_class)