Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2012 16:49
Show Gist options
  • Save martinsik/3981464 to your computer and use it in GitHub Desktop.
Save martinsik/3981464 to your computer and use it in GitHub Desktop.
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:
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])
Copy link

newbyca commented Nov 19, 2014

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment