This custom test suite runner will record all of the URLs accessed during your test suite. It will compare it to the list of all URLs you have configured for your site and produce a report of any URLs missed. It requires that all URLs are named (using the name=
parameter).
To use is, set the TEST_RUNNER
variable in your configuration to this class. You can also define ignored URLs. For example, to filter out the admin URLs, you can use:
IGNORED_COVERAGE_URLS = ['^admin/',
'^admin/doc/']
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings
from django.core.urlresolvers import resolve, get_resolver
IGNORED_COVERAGE_URLS = getattr(settings, 'IGNORED_COVERAGE_URLS', [])
def get_configured_url_names(urllist=None):
"""Recurses through django url resolvers to get a full
list of all url names"""
if urllist is None:
urllist = get_resolver(None).url_patterns
unique_names = set()
for entry in urllist:
name = getattr(entry, 'name', None)
if entry.regex.pattern in IGNORED_COVERAGE_URLS:
continue
if name is not None:
unique_names.add(name)
if hasattr(entry, 'url_patterns'):
unique_names.update(get_configured_url_names(entry.url_patterns))
return unique_names
unique_url_names = set()
class UrlMiddlewareLogger(object):
def process_request(self, request):
unique_url_names.add(resolve(request.path).url_name)
class CustomTestRunner(DjangoTestSuiteRunner):
def __init__(self, *args, **kwargs):
super(CustomTestRunner, self).__init__(*args, **kwargs)
def run_tests(self, test_labels, **kwargs):
# setup custom middleware class that records urls accessed
orig_middleware = getattr(settings, 'MIDDLEWARE_CLASSES', [])
middleware = tuple(list(orig_middleware) +
[__name__ + '.UrlMiddlewareLogger'])
setattr(settings, 'MIDDLEWARE_CLASSES', middleware)
test_results = super(CustomTestRunner, self).run_tests(test_labels, **kwargs)
# reset middleware back to what it was
setattr(settings, 'MIDDLEWARE_CLASSES', orig_middleware)
configured_names = get_configured_url_names()
missed_names = sorted(configured_names.difference(unique_url_names))
hit_names = configured_names.intersection(unique_url_names)
print '-----------------------------------------------------'
print 'Status: %d of %d URLs hit in test suite' % (len(hit_names), len(configured_names))
print '-----------------------------------------------------'
if len(missed_names) > 0:
print 'URL Names in URL Configuration not hit in test suite:'
print '-----------------------------------------------------'
for missed_name in missed_names:
print ' ' + missed_name
print
return test_results
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.