Login

Unit Test Profiling

Author:
justquick
Posted:
February 8, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.