blob: 2adaed0b70d050b1769c6e207bf9809952470d5a [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
commit-bot@chromium.org164052e2014-02-07 18:41:49 +000014import sys
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000015import unittest
16
commit-bot@chromium.org164052e2014-02-07 18:41:49 +000017# Set the PYTHONPATH to include the tools directory.
18sys.path.append(
19 os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
20import find_run_binary
21
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000022
23class TestCase(unittest.TestCase):
24
25 def shortDescription(self):
26 """Tell unittest framework to not print docstrings for test cases."""
27 return None
28
29 def run_command(self, args):
30 """Runs a program from the command line and returns stdout.
31
32 Args:
33 args: Command line to run, as a list of string parameters. args[0] is the
34 binary to run.
35
36 Returns:
37 stdout from the program, as a single string.
38
39 Raises:
40 Exception: the program exited with a nonzero return code.
41 """
commit-bot@chromium.org164052e2014-02-07 18:41:49 +000042 return find_run_binary.run_command(args)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000043
44 def find_path_to_program(self, program):
45 """Returns path to an existing program binary.
46
47 Args:
48 program: Basename of the program to find (e.g., 'render_pictures').
49
50 Returns:
51 Absolute path to the program binary, as a string.
52
53 Raises:
54 Exception: unable to find the program binary.
55 """
commit-bot@chromium.org164052e2014-02-07 18:41:49 +000056 return find_run_binary.find_path_to_program(program)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000057
58
59def main(test_case_class):
60 """Run the unit tests within the given class.
61
62 Raises an Exception if any of those tests fail (in case we are running in the
63 context of run_all.py, which depends on that Exception to signal failures).
64
65 TODO(epoger): Make all of our unit tests use the Python unittest framework,
66 so we can leverage its ability to run *all* the tests and report failures at
67 the end.
68 """
69 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class)
70 results = unittest.TextTestRunner(verbosity=2).run(suite)
71 if not results.wasSuccessful():
72 raise Exception('failed unittest %s' % test_case_class)