s4-python: Install external included packages only if they're not present on the...
[nivanova/samba-autobuild/.git] / lib / testtools / utils.py
1 # Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
2
3 """Utilities for dealing with stuff in unittest."""
4
5
6 import sys
7
8 __metaclass__ = type
9 __all__ = [
10     'iterate_tests',
11     ]
12
13
14 if sys.version_info > (3, 0):
15     def _u(s):
16         """Replacement for u'some string' in Python 3."""
17         return s
18     def _b(s):
19         """A byte literal."""
20         return s.encode("latin-1")
21     advance_iterator = next
22 else:
23     def _u(s):
24         return unicode(s, "latin-1")
25     def _b(s):
26         return s
27     advance_iterator = lambda it: it.next()
28
29
30 def iterate_tests(test_suite_or_case):
31     """Iterate through all of the test cases in 'test_suite_or_case'."""
32     try:
33         suite = iter(test_suite_or_case)
34     except TypeError:
35         yield test_suite_or_case
36     else:
37         for test in suite:
38             for subtest in iterate_tests(test):
39                 yield subtest