Skip to content

Commit 9bced60

Browse files
committed
Added a top-level test module which will run through django URLs in any
committed file named 'testurl.list' in any directory in the ietf/ tree. There is also a test for URL coverage -- if there isn't declared test URLs for all the url patterns in ietf/urls.py this coverage test will fail. - Legacy-Id: 215
1 parent 1d54ad3 commit 9bced60

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

ietf/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
# Override this in settings_local.py if it's not true
129129
SERVER_MODE = 'development'
130130

131+
# The name of the method to use to invoke the test suite
132+
TEST_RUNNER = 'ietf.tests.run_tests'
133+
131134
# Put SECRET_KEY in here, or any other sensitive or site-specific
132135
# changes. DO NOT commit settings_local.py to svn.
133136
from settings_local import *

ietf/tests.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import re
3+
4+
import django.test.simple
5+
from django.test import TestCase
6+
from ietf.urls import urlpatterns
7+
import ietf.settings
8+
import ietf.tests
9+
10+
def run_tests(module_list, verbosity=1, extra_tests=[]):
11+
module_list.append(ietf.tests)
12+
return django.test.simple.run_tests(module_list, verbosity, extra_tests)
13+
14+
class UrlTestCase(TestCase):
15+
def setUp(self):
16+
from django.test.client import Client
17+
self.client = Client()
18+
19+
# find test urls
20+
self.testurls = []
21+
for root, dirs, files in os.walk(ietf.settings.BASE_DIR):
22+
if "testurl.list" in files:
23+
filename = root+"/testurl.list" # yes, this is non-portable
24+
file = open(filename)
25+
for line in file:
26+
urlspec = line.split()
27+
if len(urlspec) == 2:
28+
code, testurl = urlspec
29+
goodurl = None
30+
elif len(urlspec) == 3:
31+
code, testurl, goodurl = urlspec
32+
else:
33+
raise ValueError("Expected 'HTTP_CODE TESTURL [GOODURL]' in %s line, found '%s'." % (filename, line))
34+
self.testurls += [ (code, testurl, goodurl) ]
35+
36+
def testCoverage(self):
37+
covered = []
38+
patterns = [pattern.regex.pattern for pattern in urlpatterns]
39+
for code, testurl, goodurl in self.testurls:
40+
for pattern in patterns:
41+
if re.match(pattern, testurl):
42+
self.covered.append(pattern)
43+
# We should have at least one test case for each url pattern declared
44+
# in our Django application:
45+
self.assertEqual(set(patterns), set(covered), "Not all the application URLs has test cases.")
46+
47+
def testUrls(self):
48+
for code, testurl, goodurl in self.testurls:
49+
response = self.client.get(testurl)
50+
self.assertEqual(response.status_code, code, "Unexpected response code from URL '%s'" % (testurl))
51+
# TODO: Add comparison with goodurl

0 commit comments

Comments
 (0)