Cleaned up exception message checking in some tests.

This commit is contained in:
Jon Dufresne 2019-03-15 16:27:57 -07:00 committed by Tim Graham
parent 58ad030d05
commit 95b7699ffc
6 changed files with 15 additions and 9 deletions

View File

@ -854,7 +854,7 @@ class LoginRedirectAuthenticatedUser(AuthViewsTestCase):
self.login()
msg = (
"Redirection loop for authenticated user detected. Check that "
"your LOGIN_REDIRECT_URL doesn't point to a login page"
"your LOGIN_REDIRECT_URL doesn't point to a login page."
)
with self.settings(LOGIN_REDIRECT_URL=self.do_redirect_url):
with self.assertRaisesMessage(ValueError, msg):

View File

@ -385,8 +385,8 @@ class FileUploadTests(TestCase):
file.write(b'a' * (2 ** 21))
file.seek(0)
# AttributeError: You cannot alter upload handlers after the upload has been processed.
with self.assertRaises(AttributeError):
msg = 'You cannot alter upload handlers after the upload has been processed.'
with self.assertRaisesMessage(AttributeError, msg):
self.client.post('/quota/broken/', {'f': file})
def test_fileupload_getlist(self):

View File

@ -63,7 +63,8 @@ class ManyToManyTests(TestCase):
)
# Adding an object of the wrong type raises TypeError
with self.assertRaisesMessage(TypeError, "'Publication' instance expected, got <Article"):
msg = "'Publication' instance expected, got <Article: Django lets you create Web apps easily>"
with self.assertRaisesMessage(TypeError, msg):
with transaction.atomic():
a6.publications.add(a5)

View File

@ -829,8 +829,9 @@ class ClientTest(TestCase):
def test_response_raises_multi_arg_exception(self):
"""A request may raise an exception with more than one required arg."""
with self.assertRaises(TwoArgException):
with self.assertRaises(TwoArgException) as cm:
self.client.get('/two_arg_exception/')
self.assertEqual(cm.exception.args, ('one', 'two'))
def test_uploading_temp_file(self):
with tempfile.TemporaryFile() as test_file:

View File

@ -997,8 +997,11 @@ class RequestURLconfTests(SimpleTestCase):
Test reversing an URL from the *default* URLconf from inside
a response middleware.
"""
message = "Reverse for 'outer' not found."
with self.assertRaisesMessage(NoReverseMatch, message):
msg = (
"Reverse for 'outer' not found. 'outer' is not a valid view "
"function or pattern name."
)
with self.assertRaisesMessage(NoReverseMatch, msg):
self.client.get('/second_test/')
@override_settings(
@ -1070,7 +1073,8 @@ class DefaultErrorHandlerTests(SimpleTestCase):
response = self.client.get('/test/')
self.assertEqual(response.status_code, 404)
with self.assertRaisesMessage(ValueError, "I don't think I'm getting good"):
msg = "I don't think I'm getting good value for this view"
with self.assertRaisesMessage(ValueError, msg):
self.client.get('/bad_view/')

View File

@ -286,7 +286,7 @@ class TestRaiseLastException(SimpleTestCase):
exc_info = sys.exc_info()
with mock.patch('django.utils.autoreload._exception', exc_info):
with self.assertRaises(MyException, msg='Test Message'):
with self.assertRaisesMessage(MyException, 'Test Message'):
autoreload.raise_last_exception()