4 Copyright 2014 Google Inc.
6 Use of this source code is governed by a BSD-style license that can be
7 found in the LICENSE file.
9 A wrapper around the standard Python unittest library, adding features we need
10 for various unittests within this directory.
12 TODO(epoger): Move this into the common repo for broader use? Or at least in
13 a more common place within the Skia repo?
23 TRUNK_DIR = os.path.abspath(os.path.join(
24 os.path.dirname(__file__), os.pardir, os.pardir))
27 class TestCase(unittest.TestCase):
29 def __init__(self, *args, **kwargs):
30 super(TestCase, self).__init__(*args, **kwargs)
31 # Subclasses should override this default value if they want their output
32 # to be automatically compared against expectations (see setUp and tearDown)
33 self._testdata_dir = None
36 """Called before each test."""
37 # Get the name of this test, in such a way that it will be consistent
38 # regardless of the directory it is run from (throw away package names,
40 self._test_name = '.'.join(self.id().split('.')[-3:])
42 self._temp_dir = tempfile.mkdtemp()
43 if self._testdata_dir:
44 self.create_empty_dir(self.output_dir_actual)
47 """Called after each test."""
48 shutil.rmtree(self._temp_dir)
49 if self._testdata_dir and os.path.exists(self.output_dir_expected):
50 different_files = _find_different_files(self.output_dir_actual,
51 self.output_dir_expected)
52 # Don't add any cleanup code below this assert!
53 # Then if tests fail, the artifacts will not be cleaned up.
54 assert (not different_files), \
55 ('found differing files:\n' +
56 '\n'.join(['tkdiff %s %s &' % (
57 os.path.join(self.output_dir_actual, basename),
58 os.path.join(self.output_dir_expected, basename))
59 for basename in different_files]))
67 assert self._testdata_dir, 'self._testdata_dir must be set'
68 return os.path.join(self._testdata_dir, 'inputs')
71 def output_dir_actual(self):
72 assert self._testdata_dir, 'self._testdata_dir must be set'
74 self._testdata_dir, 'outputs', 'actual', self._test_name)
77 def output_dir_expected(self):
78 assert self._testdata_dir, 'self._testdata_dir must be set'
80 self._testdata_dir, 'outputs', 'expected', self._test_name)
82 def shortDescription(self):
83 """Tell unittest framework to not print docstrings for test cases."""
86 def create_empty_dir(self, path):
87 """Creates an empty directory at path and returns path.
90 path: path on local disk
92 # Delete the old one, if any.
93 if os.path.isdir(path):
94 shutil.rmtree(path=path, ignore_errors=True)
95 elif os.path.lexists(path):
101 except OSError as exc:
102 # Guard against race condition (somebody else is creating the same dir)
103 if exc.errno != errno.EEXIST:
108 def _find_different_files(dir1, dir2, ignore_subtree_names=None):
109 """Returns a list of any files that differ between the directory trees rooted
113 dir1: root of a directory tree; if nonexistent, will raise OSError
114 dir2: root of another directory tree; if nonexistent, will raise OSError
115 ignore_subtree_names: list of subtree directory names to ignore;
116 defaults to ['.svn'], so all SVN files are ignores
118 TODO(epoger): include the dirname within each filename (not just the
119 basename), to make it easier to locate any differences
122 if ignore_subtree_names is None:
123 ignore_subtree_names = ['.svn']
124 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names)
125 differing_files.extend(dircmp.left_only)
126 differing_files.extend(dircmp.right_only)
127 differing_files.extend(dircmp.common_funny)
128 differing_files.extend(dircmp.diff_files)
129 differing_files.extend(dircmp.funny_files)
130 for common_dir in dircmp.common_dirs:
131 differing_files.extend(_find_different_files(
132 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir)))
133 return differing_files
136 def main(test_case_class):
137 """Run the unit tests within the given class.
139 Raises an Exception if any of those tests fail (in case we are running in the
140 context of run_all.py, which depends on that Exception to signal failures).
142 suite = unittest.TestLoader().loadTestsFromTestCase(test_case_class)
143 results = unittest.TextTestRunner(verbosity=2).run(suite)
144 if not results.wasSuccessful():
145 raise Exception('failed unittest %s' % test_case_class)