commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | A wrapper around the standard Python unittest library, adding features we need |
| 10 | for various unittests within this directory. |
| 11 | """ |
| 12 | |
| 13 | import os |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame^] | 14 | import sys |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 15 | import unittest |
| 16 | |
commit-bot@chromium.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame^] | 17 | # Set the PYTHONPATH to include the tools directory. |
| 18 | sys.path.append( |
| 19 | os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) |
| 20 | import find_run_binary |
| 21 | |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 22 | |
| 23 | class 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.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame^] | 42 | return find_run_binary.run_command(args) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 43 | |
| 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.org | 164052e | 2014-02-07 18:41:49 +0000 | [diff] [blame^] | 56 | return find_run_binary.find_path_to_program(program) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def 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) |