Login

Unit Test Profiling

Author:
justquick
Posted:
February 8, 2009
Language:
Python
Version:
1.0
Tags:
profile unittest cprofile
Score:
1 (after 1 ratings)

A test runner for Django unittests which profiles the tests run, and saves the result. Very useful for diagnosing your apps. Place the top portion of the code into a file called profiling.py someplace in your python path. Update your settings.py file with the bottom two lines of the code. Now you are ready, so just run python manage.py test [appnames...] to test any apps listed with profiling. By default this will just print a nice report after the unittests. If you change the value of TEST_PROFILE to a file, the profile will be saved to that file. The latter is recommended because these profiling reports have a lot of info in them, so it is best to tear through them with the pstats module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# file: profiling.py #
from django.test.simple import run_tests
from django.conf import settings
try:
    import cProfile as profile
except ImportError:
    import profile

def profile_tests(*args, **kwargs):
    profile.runctx('run_tests(*args, **kwargs)',
                {'run_tests':run_tests,'args':args,'kwargs':kwargs},
                {},
                getattr(settings,'TEST_PROFILE',None)
            )

# file: settings.py #
TEST_RUNNER = 'profiling.profile_tests'
TEST_PROFILE = None

More like this

  1. Dynamic Test Loading by cronosa 5 years, 6 months ago
  2. Simple profile middleware by limodou 8 years, 5 months ago
  3. Unit Test Profiling for Django 1.3/1.4 by hoffmaje 3 years, 3 months ago
  4. Query printer coroutine by fnl 6 years, 3 months ago
  5. change settings locally in an individual test by akaihola 4 years, 11 months ago

Comments

Please login first before commenting.