Code cleanup and added test filename to results.

parent c606e57c
#
# Python unit test runner
#
# Author : Sander Mathijs van Veen <smvv@kompiler.org>
# License: GPL version 3, see also the file `LICENSE'.
#
from runner import TextTestRunner, main from runner import TextTestRunner, main
__all__ = ['TextTestRunner', 'main'] __all__ = ['TextTestRunner', 'main']
...@@ -6,9 +6,7 @@ ...@@ -6,9 +6,7 @@
# #
import sys import sys
import unittest from testrunner import main
from testrunner import TextTestRunner, main
if len(sys.argv) < 2: if len(sys.argv) < 2:
print 'Usage: %s tests/test_COMPONENT.py' % sys.argv[0] print 'Usage: %s tests/test_COMPONENT.py' % sys.argv[0]
......
#
# Python unit test runner
#
# Author : Sander Mathijs van Veen <smvv@kompiler.org>
# License: GPL version 3, see also the file `LICENSE'.
#
import os import os
import sys import sys
import time import time
...@@ -24,6 +30,7 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -24,6 +30,7 @@ class TextTestRunner(unittest.TextTestRunner):
if startTestRun is not None: if startTestRun is not None:
startTestRun() startTestRun()
try: try:
test(result) test(result)
finally: finally:
...@@ -35,11 +42,12 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -35,11 +42,12 @@ class TextTestRunner(unittest.TextTestRunner):
timeTaken = stopTime - startTime timeTaken = stopTime - startTime
result.printErrors() result.printErrors()
run = result.testsRun self.stream.write('%s: %d test%s in %.3fs ' \
msg = 'Ran %d test%s in %.3fs ' % (test.filename, result.testsRun,
self.stream.write(msg % (run, run != 1 and 's' or '', timeTaken)) result.testsRun != 1 and 's' or '', timeTaken))
expectedFails = unexpectedSuccesses = skipped = 0 expectedFails = unexpectedSuccesses = skipped = 0
try: try:
results = map(len, (result.expectedFailures, results = map(len, (result.expectedFailures,
result.unexpectedSuccesses, result.unexpectedSuccesses,
...@@ -59,12 +67,14 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -59,12 +67,14 @@ class TextTestRunner(unittest.TextTestRunner):
self.stream.write(msg) self.stream.write(msg)
failed, errored = map(len, (result.failures, result.errors)) failed, errored = map(len, (result.failures, result.errors))
if failed: if failed:
if self.color: if self.color:
msg = 'failures=\033[1;31m%d\033[1;m' msg = 'failures=\033[1;31m%d\033[1;m'
else: else:
msg = 'failures=%d' msg = 'failures=%d'
infos.append(msg % failed) infos.append(msg % failed)
if errored: if errored:
if self.color: if self.color:
msg = 'errors=\033[1;31m%d\033[1;m' msg = 'errors=\033[1;31m%d\033[1;m'
...@@ -76,6 +86,7 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -76,6 +86,7 @@ class TextTestRunner(unittest.TextTestRunner):
msg = '\033[0;32mOK\033[0;m' msg = '\033[0;32mOK\033[0;m'
else: else:
msg = 'OK' msg = 'OK'
self.stream.write(msg) self.stream.write(msg)
if skipped: if skipped:
...@@ -83,6 +94,7 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -83,6 +94,7 @@ class TextTestRunner(unittest.TextTestRunner):
msg = 'skipped=\033[1;33m%d\033[1;m' msg = 'skipped=\033[1;33m%d\033[1;m'
else: else:
msg = 'skipped=%d' msg = 'skipped=%d'
infos.append(msg % skipped) infos.append(msg % skipped)
if expectedFails: if expectedFails:
...@@ -99,28 +111,38 @@ class TextTestRunner(unittest.TextTestRunner): ...@@ -99,28 +111,38 @@ class TextTestRunner(unittest.TextTestRunner):
return result return result
def main(tests, verbose=0, color=True): def main(tests, verbose=0, color=True):
suites = [] testcases = []
# Dynamic load the requested module containing the test cases. # Dynamic load the requested module containing the test cases.
for testfile in tests: for testfile in tests:
module_dir, module_file = os.path.split(testfile) try:
module_name, module_ext = os.path.splitext(module_file) module_name = os.path.splitext(testfile)[0].replace('/', '.')
module_obj = __import__(module_name)
# XXX: this looks really hacky, so cleanup is necessary. except:
module_obj = __import__(module_dir.replace('/', '.'), print 'testfile: ', testfile
fromlist=[module_name]) print 'module_name:', module_name
module_obj.__file__ = testfile raise
globals()[module_name] = module_obj
# Start the test runner and display the results to stdout. # Start the test runner and display the results to stdout.
container = module_obj.__dict__[module_name] try:
class_parts = module_name[5:].split('_') suite_name = module_name.split('.')[-1]
container = module_obj.__dict__[suite_name]
except:
print 'testfile: ', testfile
print 'module_name:', module_name
print 'module_obj: ', module_obj, dir(module_obj)
raise
# Convert lowercase, underscored suite name to Python class name.
class_parts = suite_name[5:].split('_')
class_name = 'Test' + ''.join([p.capitalize() for p in class_parts]) class_name = 'Test' + ''.join([p.capitalize() for p in class_parts])
testcase = container.__dict__[class_name] testcase = container.__dict__[class_name]
suites += [unittest.TestLoader().loadTestsFromTestCase(testcase)] testcases += [unittest.TestLoader().loadTestsFromTestCase(testcase)]
# Create the text runner and execute the tests. # Create the text runner and execute the tests.
runner = TextTestRunner(verbosity=verbose, color=color) runner = TextTestRunner(verbosity=verbose, color=color)
runner.run(unittest.TestSuite(suites)) suite = unittest.TestSuite(testcases)
suite.filename = testfile
runner.run(suite)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment