Last active
December 17, 2015 07:49
-
-
Save mstepniowski/5575853 to your computer and use it in GitHub Desktop.
Custom Django test suite runners, which can be used to speed up your test suite.
This file contains hidden or 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
# Copyright (c) 2011-2013, Marek Stepniowski | |
# All rights reserved. (FreeBSD License). | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
# The views and conclusions contained in the software and documentation are those | |
# of the authors and should not be interpreted as representing official policies, | |
# either expressed or implied, of the FreeBSD Project. | |
""" | |
Custom Django test suite runners, which can be used to speed up your test suite: | |
- PickyTestSuiteRunner - Skips TEST_EXCLUDED_APPS when running all tests. | |
- TimingTestSuiteRunner - Reports tests that took more than TEST_MAX_TIME sec. | |
- SetJamTestSuiteRunner - PickyTestSuiteRunner and TimingTestSuiteRunner COMBINED! | |
Requires Django 1.3 or later. | |
""" | |
import time | |
from django.conf import settings | |
from django.test.simple import DjangoTestSuiteRunner | |
from django.utils import unittest | |
class TimingTestResult(unittest.TextTestResult): | |
def __init__(self, timings, stream, descriptions, verbosity): | |
super(TimingTestResult, self).__init__(stream, descriptions, verbosity) | |
self.timings = timings | |
self.started = None | |
def startTest(self, test): | |
super(TimingTestResult, self).startTest(test) | |
self.started = time.time() | |
def stopTest(self, test): | |
execution_time = time.time() - self.started | |
super(TimingTestResult, self).stopTest(test) | |
if execution_time > getattr(settings, 'TEST_MAX_TIME', 1.0): | |
self.timings.append((execution_time, self.getDescription(test))) | |
def stopTestRun(self): | |
super(TimingTestResult, self).stopTestRun() | |
if len(self.timings) > 0: | |
self.stream.write('\n') | |
self.stream.write('-' * 70) | |
self.stream.write('\nTests that took too much time to execute:\n') | |
self.stream.write('-' * 70) | |
self.stream.write('\n') | |
self.timing_table.sort(reverse=True) | |
for execution_time, description in self.timings: | |
self.stream.write('%s: %f seconds\n' % (description, | |
execution_time)) | |
self.stream.write('\n') | |
self.stream.flush() | |
class TimingTestRunner(unittest.TextTestRunner): | |
def __init__(self, verbosity=0, failfast=False, **kwargs): | |
super(unittest.TimingTestRunner, self).__init__(verbosity=verbosity, | |
failfast=failfast, | |
**kwargs) | |
self.timings = [] | |
def _makeResult(self): | |
result = TimingTestResult(self.timings, | |
self.stream, | |
self.descriptions, | |
self.verbosity) | |
failfast = self.failfast | |
def stoptest_override(func): | |
def stoptest(test): | |
# If we were set to failfast and the unit test failed, | |
# or if the user has typed Ctrl-C, report and quit | |
if failfast and not result.wasSuccessful(): | |
result.stop() | |
func(test) | |
return stoptest | |
setattr(result, 'stopTest', stoptest_override(result.stopTest)) | |
return result | |
class PickyTestSuiteRunner(DjangoTestSuiteRunner): | |
"""Test runner that skips TEST_EXCLUDED_APPS when running whole test suite. | |
PickyTestSuiteRunner can speed up your test suite by not testing | |
repeatedly Django contrib and 3rd party applications. | |
To use, add the following lines to your settings.py file:: | |
TEST_RUNNER = 'testing.PickyTestSuiteRunner' | |
TEST_EXCLUDED_APPS = [ | |
'django.contrib.admindocs', | |
'django.contrib.sitemaps', | |
'django.contrib.flatpages', | |
# Other applications you don't want to test | |
] | |
""" | |
def run_tests(self, test_labels, extra_tests=None, **kwargs): | |
if test_labels is None: | |
excluded_apps = getattr(settings, 'TEST_EXCLUDED_APPS', ()) | |
test_labels= [app_path.split('.')[-1] | |
for app_path in settings.INSTALLED_APPS | |
if app_path not in excluded_apps] | |
return super(TimingTestSuiteRunner, self).run_tests( | |
test_labels, extra_tests, **kwargs) | |
class TimingTestSuiteRunner(DjangoTestSuiteRunner): | |
"""Test runner reporting tests that run for more than TEST_MAX_TIME seconds. | |
TimingTestSuiteRunner can speed up your test suite by publicly | |
blaming coworkers who shamelessly commit tests taking too much | |
time to execute. | |
To use, add the following lines to your settings.py file:: | |
TEST_RUNNER = 'testing.TimingTestSuiteRunner' | |
# Max time that a single test execution should take (in seconds) | |
TEST_MAX_TIME = 1.0 | |
""" | |
def run_suite(self, suite, **kwargs): | |
runner = TimingTestRunner(verbosity=self.verbosity, | |
failfast=self.failfast) | |
return runner.run(suite) | |
def SetJamTestSuiteRunner(PickyTestSuiteRunner, TimingTestSuiteRunner): | |
"""Powers of PickyTestSuiteRunner and TimingTestSuiteRunner COMBINED!""" | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment