Created
October 30, 2012 16:49
-
-
Save martinsik/3981464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import fnmatch | |
import unittest | |
import webapp2 | |
import importlib | |
from time import clock | |
class RunUnitTests(webapp2.RequestHandler): | |
def get(self): | |
self.response.headers['Content-Type'] = 'text/plain' | |
suite = unittest.TestSuite() | |
loader = unittest.TestLoader() | |
testCases = self._findTestCases() | |
#print testCases | |
for testCase in testCases: | |
suite.addTests(loader.loadTestsFromTestCase(testCase)) | |
startTime = clock() | |
result = unittest.TextTestRunner(verbosity=2).run(suite) | |
stopTime = clock() | |
self.response.out.write(('Test cases (%d):\n' % len(testCases)) + '\n'.join(map(repr, testCases)) + '\n\n') | |
self._printTestResultsGroup(result, 'errors') | |
self._printTestResultsGroup(result, 'failures') | |
self.response.out.write('Total tests: %d\n' % result.testsRun) | |
self.response.out.write('Status: %s (%s ms)\n' % ('OK' if result.wasSuccessful() else 'FAILED', (stopTime - startTime) * 1000)) | |
def _findTestCases(self): | |
testCases = [] | |
for root, dirnames, filenames in os.walk('.'): | |
for filename in fnmatch.filter(filenames, 'Test*.py'): | |
classPath = os.path.splitext(os.path.join(root, filename)[2:])[0].replace('/', '.') | |
className = os.path.splitext(filename)[0] | |
testCases.append(getattr(importlib.import_module(classPath), className)) | |
return testCases | |
def _printTestResultsGroup(self, result, name): | |
list = getattr(result, name) | |
if len(list): | |
self.response.out.write("%s (%d):\n" % (name.capitalize(), len(list))) | |
for item in list: | |
self.response.out.write('%s\n' % item[0]) | |
self.response.out.write('\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gist! :) Suggestion: on line 36, replace '/' with os.sep for platform compat. There's no pull request for gists (afaik), but I made the change here if you like:
https://gist.github.com/newbyca/ef3aabe5f7712e2bb1ef