From 9bced60e2f0ba70667edf2aa96c999a2365c818b Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Sun, 3 Jun 2007 19:52:06 +0000 Subject: [PATCH] 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 --- ietf/settings.py | 3 +++ ietf/tests.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 ietf/tests.py diff --git a/ietf/settings.py b/ietf/settings.py index 016a959d1..c0f898caf 100644 --- a/ietf/settings.py +++ b/ietf/settings.py @@ -128,6 +128,9 @@ INTERNAL_IPS = ( # Override this in settings_local.py if it's not true SERVER_MODE = 'development' +# The name of the method to use to invoke the test suite +TEST_RUNNER = 'ietf.tests.run_tests' + # Put SECRET_KEY in here, or any other sensitive or site-specific # changes. DO NOT commit settings_local.py to svn. from settings_local import * diff --git a/ietf/tests.py b/ietf/tests.py new file mode 100644 index 000000000..a17adc86f --- /dev/null +++ b/ietf/tests.py @@ -0,0 +1,51 @@ +import os +import re + +import django.test.simple +from django.test import TestCase +from ietf.urls import urlpatterns +import ietf.settings +import ietf.tests + +def run_tests(module_list, verbosity=1, extra_tests=[]): + module_list.append(ietf.tests) + return django.test.simple.run_tests(module_list, verbosity, extra_tests) + +class UrlTestCase(TestCase): + def setUp(self): + from django.test.client import Client + self.client = Client() + + # find test urls + self.testurls = [] + for root, dirs, files in os.walk(ietf.settings.BASE_DIR): + if "testurl.list" in files: + filename = root+"/testurl.list" # yes, this is non-portable + file = open(filename) + for line in file: + urlspec = line.split() + if len(urlspec) == 2: + code, testurl = urlspec + goodurl = None + elif len(urlspec) == 3: + code, testurl, goodurl = urlspec + else: + raise ValueError("Expected 'HTTP_CODE TESTURL [GOODURL]' in %s line, found '%s'." % (filename, line)) + self.testurls += [ (code, testurl, goodurl) ] + + def testCoverage(self): + covered = [] + patterns = [pattern.regex.pattern for pattern in urlpatterns] + for code, testurl, goodurl in self.testurls: + for pattern in patterns: + if re.match(pattern, testurl): + self.covered.append(pattern) + # We should have at least one test case for each url pattern declared + # in our Django application: + self.assertEqual(set(patterns), set(covered), "Not all the application URLs has test cases.") + + def testUrls(self): + for code, testurl, goodurl in self.testurls: + response = self.client.get(testurl) + self.assertEqual(response.status_code, code, "Unexpected response code from URL '%s'" % (testurl)) + # TODO: Add comparison with goodurl