1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Test utilities
21 """
22
23 __docformat__ = 'reStructuredText'
24
25 import unittest
26 import os
27 import logging
28
29 _logger = logging.getLogger("pytilities.testing")
30
32 """
33 Get a test suite of all tests inside a package and its child packages
34
35 Parameters:
36
37 `root`:: string
38 absolute path of the top package's directory of this python program
39
40 `test_package`:: string
41 name of package to search in for tests, in dotted format, e.g.
42 'project.test'
43
44 See the wiki for a usage example.
45
46 Returns ::unittest.TestSuite
47 """
48 test_suite = unittest.TestSuite()
49
50 for parent, dirs, files in os.walk(
51 os.path.join(root, test_package.replace('.', os.path.sep))):
52
53 if parent == '.svn' or '__init__.py' not in files:
54 continue
55
56 for file_ in files:
57 if os.path.splitext(file_)[1] != '.py':
58 continue
59
60 file_path = os.path.join(parent[len(root)+1:],
61 file_[:-3])
62
63 module_full_name = file_path.replace(os.sep, '.')
64
65 _logger.debug('Loading: %s' % module_full_name)
66
67 test_suite.addTest(
68 unittest.defaultTestLoader.loadTestsFromName(module_full_name))
69
70 return test_suite
71
73 """
74 Run test and print results to stderr
75
76 Parameters:
77
78 `test`
79 the TestCase to run
80 """
81 runner = unittest.TextTestRunner()
82 return runner.run(test)
83