Skip to content

Instantly share code, notes, and snippets.

@newbyca
Forked from martinsik/RunUnitTests.py
Created November 19, 2014 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newbyca/ef3aabe5f7712e2bb1ef to your computer and use it in GitHub Desktop.
Save newbyca/ef3aabe5f7712e2bb1ef 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:
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(os.sep, '.')
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