Changeset 5179
- Timestamp:
- 05/10/07 06:27:59 (2 years ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (1 diff)
- django/trunk/docs/testing.txt (modified) (2 diffs)
- django/trunk/tests/modeltests/test_client/models.py (modified) (3 diffs)
- django/trunk/tests/modeltests/test_client/urls.py (modified) (2 diffs)
- django/trunk/tests/modeltests/test_client/views.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r5173 r5179 57 57 super(TestCase, self).run(result) 58 58 59 def assertRedirects(self, response, expected_path ):60 """Assert that a response redirected to a specific URL, and that the 59 def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200): 60 """Assert that a response redirected to a specific URL, and that the 61 61 redirect URL can be loaded. 62 62 63 63 """ 64 self.assertEqual(response.status_code, 302, 65 "Response didn't redirect: Reponse code was %d" % response.status_code) 64 self.assertEqual(response.status_code, status_code, 65 "Response didn't redirect: Reponse code was %d (expected %d)" % 66 (response.status_code, status_code)) 66 67 scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 67 68 self.assertEqual(path, expected_path, 68 69 "Response redirected to '%s', expected '%s'" % (path, expected_path)) 69 70 redirect_response = self.client.get(path) 70 self.assertEqual(redirect_response.status_code, 200, 71 "Couldn't retrieve redirection page '%s'" % path) 71 self.assertEqual(redirect_response.status_code, target_status_code, 72 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % 73 (path, response.status_code, status_code)) 72 74 73 def assertContains(self, response, text, count=1 ):75 def assertContains(self, response, text, count=1, status_code=200): 74 76 """Assert that a response indicates that a page was retreived successfully, 75 (i.e., the HTTP status code was 200), and that ``text`` occurs ``count``77 (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 76 78 times in the content of the response. 77 79 78 80 """ 79 self.assertEqual(response.status_code, 200, 80 "Couldn't retrieve page'") 81 self.assertEqual(response.status_code, status_code, 82 "Couldn't retrieve page: Response code was %d (expected %d)'" % 83 (response.status_code, status_code)) 81 84 real_count = response.content.count(text) 82 85 self.assertEqual(real_count, count, 83 " Could only find %d of %d instances of '%s' in response" % (real_count, count, text))84 86 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 87 85 88 def assertFormError(self, response, form, field, errors): 86 89 "Assert that a form used to render the response has a specific field error" django/trunk/docs/testing.txt
r5173 r5179 473 473 that can be useful in testing the behavior of web sites. 474 474 475 ``assertContains(response, text, count=1 )``476 Assert that a response indicates that a page was retrieved successfully,477 (i.e., the HTTP status code was 200), and that ``text`` occurs ``count``475 ``assertContains(response, text, count=1, status_code=200)`` 476 Assert that a response indicates that a page could be retrieved and 477 produced the nominated status code, and that ``text`` occurs ``count`` 478 478 times in the content of the response. 479 479 … … 494 494 the response. 495 495 496 ``assertRedirects(response, expected_path)`` 497 Assert that the response received redirects the browser to the provided 498 path, and that the expected_path can be retrieved. 496 ``assertRedirects(response, expected_path, status_code=302, target_status_code=200)`` 497 Assert that the response received produced the nominated status code, 498 redirects the browser to the provided path, and that retrieving the provided 499 path yields a response with the target status code. 499 500 500 501 ``assertTemplateUsed(response, template_name)`` django/trunk/tests/modeltests/test_client/models.py
r5173 r5179 36 36 37 37 def test_no_template_view(self): 38 " Check that template usage assersions work then templates aren't in use"38 "Template usage assertions work then templates aren't in use" 39 39 response = self.client.get('/test_client/no_template_view/') 40 40 … … 76 76 77 77 def test_raw_post(self): 78 "POST raw data (with a content type) to a view" 78 79 test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>""" 79 80 response = self.client.post("/test_client/raw_post_view/", test_doc, … … 89 90 # Check that the response was a 302 (redirect) 90 91 self.assertRedirects(response, '/test_client/get_view/') 92 93 def test_permanent_redirect(self): 94 "GET a URL that redirects permanently elsewhere" 95 response = self.client.get('/test_client/permanent_redirect_view/') 96 97 # Check that the response was a 301 (permanent redirect) 98 self.assertRedirects(response, '/test_client/get_view/', status_code=301) 99 100 def test_redirect_to_strange_location(self): 101 "GET a URL that redirects to a non-200 page" 102 response = self.client.get('/test_client/double_redirect_view/') 103 104 # Check that the response was a 302, and that 105 # the attempt to get the redirection location returned 301 when retrieved 106 self.assertRedirects(response, '/test_client/permanent_redirect_view/', target_status_code=301) 107 108 def test_notfound_response(self): 109 "GET a URL that responds as '404:Not Found'" 110 response = self.client.get('/test_client/bad_view/') 111 112 # Check that the response was a 404, and that the content contains MAGIC 113 self.assertContains(response, 'MAGIC', status_code=404) 91 114 92 115 def test_valid_form(self): django/trunk/tests/modeltests/test_client/urls.py
r5173 r5179 1 1 from django.conf.urls.defaults import * 2 from django.views.generic.simple import redirect_to 2 3 import views 3 4 … … 8 9 (r'^raw_post_view/$', views.raw_post_view), 9 10 (r'^redirect_view/$', views.redirect_view), 11 (r'^permanent_redirect_view/$', redirect_to, { 'url': '/test_client/get_view/' }), 12 (r'^double_redirect_view/$', views.double_redirect_view), 13 (r'^bad_view/$', views.bad_view), 10 14 (r'^form_view/$', views.form_view), 11 15 (r'^form_view_with_template/$', views.form_view_with_template), django/trunk/tests/modeltests/test_client/views.py
r5173 r5179 2 2 from django.core.mail import EmailMessage, SMTPConnection 3 3 from django.template import Context, Template 4 from django.http import HttpResponse, HttpResponseRedirect 4 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound 5 5 from django.contrib.auth.decorators import login_required 6 6 from django.newforms.forms import Form … … 54 54 "A view that redirects all requests to the GET view" 55 55 return HttpResponseRedirect('/test_client/get_view/') 56 57 def double_redirect_view(request): 58 "A view that redirects all requests to a redirection view" 59 return HttpResponseRedirect('/test_client/permanent_redirect_view/') 60 61 def bad_view(request): 62 "A view that returns a 404 with some error content" 63 return HttpResponseNotFound('Not found!. This page contains some MAGIC content') 56 64 57 65 TestChoices = (
