Developing a nose Test Plugin to Time Python Tests

28 February 2011

Nose is a fantastic testing framework. What surprises me though, is that there's no out of the box plugin to time tests to see which tests are the slowest, and most likely, problematic. After all, unit tests are supposed to be wicked fast. I googled, but nothing really came up except an insightful google-groups post.

I figured, what the hey, might as well just write a simple nose plugin to time the tests. I modeled it slightly off the xunit nose plugin. With the xunit plugin as a guiding example, I thought it was pretty trivial to write a nose plugin, definitely a testament to the great design chosen by nose.

Something cool about nose was that you don't have to install a plugin package-wide using setuptools, but you can actually just dynamically add it during run-time. All you have to do is:

import nose

from yourplugin import YourPlugin

if __name__ == '__main__':
    nose.main(addplugins=[YourPlugin()])

This way, you can just execute your tests as normal, like so:

python nose-testtimers.py --with-test-timer -sv --debug=sqlalchemy.engine

and you get a nice little output of test times :)

This entry was tagged as python

blog comments powered by Disqus