28

Is there a way to time the execution time of individual Python tests which are run by nosetests ?

TimStaley
  • 465
  • 3
  • 20
kamal
  • 9,015
  • 28
  • 92
  • 148

2 Answers2

27

You might try the nose plug-in posted here: https://github.com/mahmoudimus/nose-timer (or available via pip / PyPi). You can also use the built-in plugin --with-profile to do more serious profiling.

TimStaley
  • 465
  • 3
  • 20
Noah
  • 17,200
  • 7
  • 59
  • 68
  • 4
    nosetests -s --with-xunit --with-profile `find . -name "*test.py"` Usage: nosetests [options] nosetests: error: no such option: --with-profile – kamal Mar 30 '11 at 03:59
  • hrm, works for me. Are you running the latest version of nosetests? (seems to be 1.0.0) See here for info about profiling: http://somethingaboutorange.com/mrl/projects/nose/1.0.0/plugins/prof.html – Noah Mar 30 '11 at 14:23
  • Try `python -c 'from hotshot import stats'`, I think that will break on you and tell you to install the python-profiler package. – Chris Wesseling Aug 08 '11 at 04:10
  • The hotshot profiler that nose uses by default isn't really supported anymore, I believe. I wrote a plugin to use cProfile instead, which you can find here: http://pypi.python.org/pypi/nose-cprof/0.1-0 – Marc Jul 17 '12 at 19:58
  • nose-cprof not working for me. pip install looks fine, but the flag seems to be not found: no such option: --with-cprofile. --with-profile seems to work for me thoug – Priyeshj Jul 13 '16 at 20:07
10

Alternatively:

python -m cProfile -o profile.out `which nosetests` .

The output from can be viewed using, for example, runsnakerun, which makes it visually very obvious where your performance problems are. (e.g. it might be in a common method that many tests indirectly call)

Ryan Ginstrom
  • 13,275
  • 5
  • 39
  • 59
Jonathan Hartley
  • 13,922
  • 8
  • 71
  • 77