Changeset 5183
- Timestamp:
- 05/10/07 10:51:59 (2 years ago)
- Files:
-
- django/branches/boulder-oracle-sprint/django/test/simple.py (modified) (1 diff)
- django/branches/boulder-oracle-sprint/django/test/testcases.py (modified) (2 diffs)
- django/branches/boulder-oracle-sprint/docs/testing.txt (modified) (2 diffs)
- django/branches/boulder-oracle-sprint/tests/modeltests/test_client/models.py (modified) (3 diffs)
- django/branches/boulder-oracle-sprint/tests/modeltests/test_client/urls.py (modified) (1 diff)
- django/branches/boulder-oracle-sprint/tests/modeltests/test_client/views.py (modified) (2 diffs)
- django/branches/boulder-oracle-sprint/tests/regressiontests/test_client_regress (copied) (copied from django/trunk/tests/regressiontests/test_client_regress)
- django/branches/boulder-oracle-sprint/tests/regressiontests/test_client_regress/__init__.py (copied) (copied from django/trunk/tests/regressiontests/test_client_regress/__init__.py)
- django/branches/boulder-oracle-sprint/tests/regressiontests/test_client_regress/models.py (copied) (copied from django/trunk/tests/regressiontests/test_client_regress/models.py)
- django/branches/boulder-oracle-sprint/tests/regressiontests/test_client_regress/urls.py (copied) (copied from django/trunk/tests/regressiontests/test_client_regress/urls.py)
- django/branches/boulder-oracle-sprint/tests/regressiontests/test_client_regress/views.py (copied) (copied from django/trunk/tests/regressiontests/test_client_regress/views.py)
- django/branches/boulder-oracle-sprint/tests/urls.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/boulder-oracle-sprint/django/test/simple.py
r4855 r5183 50 50 else: 51 51 # The module exists, so there must be an import error in the 52 # test module itself. We don't need the module; close the file 53 # handle returned by find_module. 54 mod[0].close() 52 # test module itself. We don't need the module; so if the 53 # module was a single file module (i.e., tests.py), close the file 54 # handle returned by find_module. Otherwise, the test module 55 # is a directory, and there is nothing to close. 56 if mod[0]: 57 mod[0].close() 55 58 raise 56 59 django/branches/boulder-oracle-sprint/django/test/testcases.py
r5174 r5183 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" … … 103 106 if form in context: 104 107 found_form = True 105 try:106 for err in errors:107 if field :108 for err in errors: 109 if field: 110 if field in context[form].errors: 108 111 self.assertTrue(err in context[form].errors[field], 109 "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" % 110 (field, form, i, err, list(context[form].errors[field]))) 112 "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" % 113 (field, form, i, err, list(context[form].errors[field]))) 114 elif field in context[form].fields: 115 self.fail("The field '%s' on form '%s' in context %d contains no errors" % 116 (field, form, i)) 111 117 else: 112 self. assertTrue(err in context[form].non_field_errors(),113 "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %114 (form, i, err, list(context[form].non_field_errors())))115 except KeyError:116 self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field))118 self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) 119 else: 120 self.assertTrue(err in context[form].non_field_errors(), 121 "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" % 122 (form, i, err, list(context[form].non_field_errors()))) 117 123 if not found_form: 118 124 self.fail("The form '%s' was not used to render the response" % form) django/branches/boulder-oracle-sprint/docs/testing.txt
r5174 r5183 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/branches/boulder-oracle-sprint/tests/modeltests/test_client/models.py
r5174 r5183 35 35 self.assertEqual(response.template.name, 'GET Template') 36 36 37 def test_no_template_view(self):38 "Check that template usage assersions work then templates aren't in use"39 response = self.client.get('/test_client/no_template_view/')40 41 # Check that the no template case doesn't mess with the template assertions42 self.assertTemplateNotUsed(response, 'GET Template')43 44 37 def test_get_post_view(self): 45 38 "GET a view that normally expects POSTs" … … 76 69 77 70 def test_raw_post(self): 71 "POST raw data (with a content type) to a view" 78 72 test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>""" 79 73 response = self.client.post("/test_client/raw_post_view/", test_doc, … … 89 83 # Check that the response was a 302 (redirect) 90 84 self.assertRedirects(response, '/test_client/get_view/') 85 86 def test_permanent_redirect(self): 87 "GET a URL that redirects permanently elsewhere" 88 response = self.client.get('/test_client/permanent_redirect_view/') 89 90 # Check that the response was a 301 (permanent redirect) 91 self.assertRedirects(response, '/test_client/get_view/', status_code=301) 92 93 def test_redirect_to_strange_location(self): 94 "GET a URL that redirects to a non-200 page" 95 response = self.client.get('/test_client/double_redirect_view/') 96 97 # Check that the response was a 302, and that 98 # the attempt to get the redirection location returned 301 when retrieved 99 self.assertRedirects(response, '/test_client/permanent_redirect_view/', target_status_code=301) 100 101 def test_notfound_response(self): 102 "GET a URL that responds as '404:Not Found'" 103 response = self.client.get('/test_client/bad_view/') 104 105 # Check that the response was a 404, and that the content contains MAGIC 106 self.assertContains(response, 'MAGIC', status_code=404) 91 107 92 108 def test_valid_form(self): django/branches/boulder-oracle-sprint/tests/modeltests/test_client/urls.py
r5174 r5183 1 1 from django.conf.urls.defaults import * 2 from django.views.generic.simple import redirect_to 2 3 import views 3 4 4 5 urlpatterns = patterns('', 5 (r'^no_template_view/$', views.no_template_view),6 6 (r'^get_view/$', views.get_view), 7 7 (r'^post_view/$', views.post_view), 8 8 (r'^raw_post_view/$', views.raw_post_view), 9 9 (r'^redirect_view/$', views.redirect_view), 10 (r'^permanent_redirect_view/$', redirect_to, { 'url': '/test_client/get_view/' }), 11 (r'^double_redirect_view/$', views.double_redirect_view), 12 (r'^bad_view/$', views.bad_view), 10 13 (r'^form_view/$', views.form_view), 11 14 (r'^form_view_with_template/$', views.form_view_with_template), django/branches/boulder-oracle-sprint/tests/modeltests/test_client/views.py
r5174 r5183 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 7 7 from django.newforms import fields 8 8 from django.shortcuts import render_to_response 9 10 def no_template_view(request):11 "A simple view that expects a GET request, and returns a rendered template"12 return HttpResponse("No template used")13 9 14 10 def get_view(request): … … 54 50 "A view that redirects all requests to the GET view" 55 51 return HttpResponseRedirect('/test_client/get_view/') 52 53 def double_redirect_view(request): 54 "A view that redirects all requests to a redirection view" 55 return HttpResponseRedirect('/test_client/permanent_redirect_view/') 56 57 def bad_view(request): 58 "A view that returns a 404 with some error content" 59 return HttpResponseNotFound('Not found!. This page contains some MAGIC content') 56 60 57 61 TestChoices = ( django/branches/boulder-oracle-sprint/tests/urls.py
r4695 r5183 4 4 # test_client modeltest urls 5 5 (r'^test_client/', include('modeltests.test_client.urls')), 6 (r'^test_client_regress/', include('regressiontests.test_client_regress.urls')), 6 7 7 8 # Always provide the auth system login and logout views
