8640d5a409e51ec32f7789a354c6a10b91d91a3d
[python-bulletml.git] / __init__.py
1 """Generic test runner.
2
3 This module scans for filenames begining with test_ and runs the tests
4 in them. It is less noisy than the default Python test runners.
5 """
6
7 from __future__ import division
8 from __future__ import print_function
9
10 import glob
11 import os
12 import sys
13 import unittest
14
15 from unittest import TestCase
16
17 suites = []
18 add = suites.append
19
20 for name in glob.glob(os.path.join(os.path.dirname(__file__), "test_*.py")):
21 module = "tests." + os.path.basename(name)
22 __import__(module[:-3], {}, {}, [])
23
24 class Result(unittest.TestResult):
25
26 separator1 = '=' * 70
27 separator2 = '-' * 70
28
29 def addSuccess(self, test):
30 unittest.TestResult.addSuccess(self, test)
31 sys.stdout.write('.')
32
33 def addError(self, test, err):
34 unittest.TestResult.addError(self, test, err)
35 sys.stdout.write('E')
36
37 def addFailure(self, test, err):
38 unittest.TestResult.addFailure(self, test, err)
39 sys.stdout.write('F')
40
41 def printErrors(self):
42 succ = self.testsRun - (len(self.errors) + len(self.failures))
43 v = "%3d" % succ
44 count = 50 - self.testsRun
45 sys.stdout.write((" " * count) + v + "\n")
46 self.printErrorList('ERROR', self.errors)
47 self.printErrorList('FAIL', self.failures)
48
49 def printErrorList(self, flavour, errors):
50 for test, err in errors:
51 sys.stdout.write(self.separator1 + "\n")
52 sys.stdout.write("%s: %s\n" % (flavour, str(test)))
53 sys.stdout.write(self.separator2 + "\n")
54 sys.stdout.write("%s\n" % err)
55
56 class Runner(object):
57 def run(self, test):
58 suite = unittest.makeSuite(test)
59 pref = '%s (%d): ' % (test.__name__, len(suite._tests))
60 print(pref + " " * (25 - len(pref)), end=" ")
61 result = Result()
62 suite(result)
63 result.printErrors()
64 return bool(result.failures + result.errors)
65
66 def unit(run=[]):
67 runner = Runner()
68 failures = False
69 for test in suites:
70 if not run or test.__name__ in run or test.__name__.lstrip("T") in run:
71 failures |= runner.run(test)
72 return failures