Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I measure the execution time of python unit tests with nosetests?

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

like image 346
kamal Avatar asked Mar 30 '11 02:03

kamal


People also ask

What is nose tools in Python?

The nose. tools module provides a number of testing aids that you may find useful, including decorators for restricting test execution time and testing for exceptions, and all of the same assertX methods found in unittest.

How do I run a unit test from command line in Python?

The command to run the tests is python -m unittest filename.py . In our case, the command to run the tests is python -m unittest test_utils.py .

Which function in unit test will run all of your tests in Python?

pytest. The pytest test runner supports the execution of unittest test cases. The actual benefit of the pytest is to writing pytest test cases.


2 Answers

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.

like image 98
Noah Avatar answered Sep 19 '22 20:09

Noah


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)

like image 31
Jonathan Hartley Avatar answered Sep 16 '22 20:09

Jonathan Hartley