Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nose plugin to time tests
"""This plugin provides test timings to identify which tests might be
taking the most. From this information, it might be useful to couple
individual tests nose's `--with-profile` option to profile problematic
This plugin is heavily influenced by nose's `xunit` plugin.
Add this command to the way you execute nose::
After all tests are executed, they will be sorted in ascending order.
(c) 2011 - Mahmoud Abdelkader (
import operator
from time import time
import nose
from nose.plugins.base import Plugin
class TestTimer(Plugin):
"""This plugin provides test timings
name = 'test-timer'
score = 1
def _timeTaken(self):
if hasattr(self, '_timer'):
taken = time() - self._timer
# test died before it ran (probably error in setup())
# or success/failure added before test started probably
# due to custom TestResult munging
taken = 0.0
return taken
def options(self, parser, env):
"""Sets additional command line options."""
super(TestTimer, self).options(parser, env)
def configure(self, options, config):
"""Configures the test timer plugin."""
super(TestTimer, self).configure(options, config)
self.config = config
self._timed_tests = {}
def startTest(self, test):
"""Initializes a timer before starting a test."""
self._timer = time()
def report(self, stream):
"""Report the test times"""
if not self.enabled:
d = sorted(self._timed_tests.iteritems(), key=operator.itemgetter(1))
for test, time_taken in d:
stream.writeln("%s: %0.4f" % (test, time_taken))
def _register_time(self, test):
self._timed_tests[] = self._timeTaken()
def addError(self, test, err, capt=None):
def addFailure(self, test, err, capt=None, tb_info=None):
def addSuccess(self, test, capt=None):
if __name__ == '__main__':

This comment has been minimized.

Copy link

@mikewaters mikewaters commented Sep 25, 2013

This is fantastic, thank you.


This comment has been minimized.

Copy link
Owner Author

@mahmoudimus mahmoudimus commented Oct 11, 2013

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