Kaydet (Commit) 326949e4 authored tarafından Ramiro Morales's avatar Ramiro Morales

Fixed #14503 -- Unified multiple implementations of test cases assert* methods…

Fixed #14503 -- Unified multiple implementations of test cases assert* methods that verify a given exception is raised by a callable throughout the Django test suite.

Replaced them with a new assertRaisesMessage method of a new SimpleTestCase, a lightweight subclass of unittest.TestCase. Both are also available for usage in user tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16610 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst a539d434
...@@ -3,5 +3,6 @@ Django Unit Test and Doctest framework. ...@@ -3,5 +3,6 @@ Django Unit Test and Doctest framework.
""" """
from django.test.client import Client, RequestFactory from django.test.client import Client, RequestFactory
from django.test.testcases import TestCase, TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature from django.test.testcases import (TestCase, TransactionTestCase,
SimpleTestCase, skipIfDBFeature, skipUnlessDBFeature)
from django.test.utils import Approximate from django.test.utils import Approximate
...@@ -21,7 +21,7 @@ from django.utils import simplejson, unittest as ut2 ...@@ -21,7 +21,7 @@ from django.utils import simplejson, unittest as ut2
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
__all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase', __all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase',
'skipIfDBFeature', 'skipUnlessDBFeature') 'SimpleTestCase', 'skipIfDBFeature', 'skipUnlessDBFeature')
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)", lambda m: "Decimal(\"%s\")" % m.groups()[0], s) normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)", lambda m: "Decimal(\"%s\")" % m.groups()[0], s)
...@@ -235,8 +235,43 @@ class _AssertNumQueriesContext(object): ...@@ -235,8 +235,43 @@ class _AssertNumQueriesContext(object):
) )
) )
class SimpleTestCase(ut2.TestCase):
class TransactionTestCase(ut2.TestCase): def save_warnings_state(self):
"""
Saves the state of the warnings module
"""
self._warnings_state = get_warnings_state()
def restore_warnings_state(self):
"""
Restores the sate of the warnings module to the state
saved by save_warnings_state()
"""
restore_warnings_state(self._warnings_state)
def settings(self, **kwargs):
"""
A context manager that temporarily sets a setting and reverts
back to the original value when exiting the context.
"""
return override_settings(**kwargs)
def assertRaisesMessage(self, expected_exception, expected_message,
callable_obj=None, *args, **kwargs):
"""Asserts that the message in a raised exception matches the passe value.
Args:
expected_exception: Exception class expected to be raised.
expected_message: expected error message string value.
callable_obj: Function to be called.
args: Extra args.
kwargs: Extra kwargs.
"""
return self.assertRaisesRegexp(expected_exception,
re.escape(expected_message), callable_obj, *args, **kwargs)
class TransactionTestCase(SimpleTestCase):
# The class we'll use for the test client self.client. # The class we'll use for the test client self.client.
# Can be overridden in derived classes. # Can be overridden in derived classes.
client_class = Client client_class = Client
...@@ -332,26 +367,6 @@ class TransactionTestCase(ut2.TestCase): ...@@ -332,26 +367,6 @@ class TransactionTestCase(ut2.TestCase):
settings.ROOT_URLCONF = self._old_root_urlconf settings.ROOT_URLCONF = self._old_root_urlconf
clear_url_caches() clear_url_caches()
def save_warnings_state(self):
"""
Saves the state of the warnings module
"""
self._warnings_state = get_warnings_state()
def restore_warnings_state(self):
"""
Restores the sate of the warnings module to the state
saved by save_warnings_state()
"""
restore_warnings_state(self._warnings_state)
def settings(self, **kwargs):
"""
A context manager that temporarily sets a setting and reverts
back to the original value when exiting the context.
"""
return override_settings(**kwargs)
def assertRedirects(self, response, expected_url, status_code=302, def assertRedirects(self, response, expected_url, status_code=302,
target_status_code=200, host=None, msg_prefix=''): target_status_code=200, host=None, msg_prefix=''):
"""Asserts that a response redirected to a specific URL, and that the """Asserts that a response redirected to a specific URL, and that the
......
...@@ -480,7 +480,7 @@ setting_changed ...@@ -480,7 +480,7 @@ setting_changed
.. data:: django.test.signals.setting_changed .. data:: django.test.signals.setting_changed
:module: :module:
Sent when some :ref:`settings are overridden <overriding-setting>` with the Sent when some :ref:`settings are overridden <overriding-settings>` with the
:meth:`django.test.TestCase.setting` context manager or the :meth:`django.test.TestCase.setting` context manager or the
:func:`django.test.utils.override_settings` decorator/context manager. :func:`django.test.utils.override_settings` decorator/context manager.
......
...@@ -715,7 +715,7 @@ arguments at time of construction: ...@@ -715,7 +715,7 @@ arguments at time of construction:
The headers sent via ``**extra`` should follow CGI_ specification. The headers sent via ``**extra`` should follow CGI_ specification.
For example, emulating a different "Host" header as sent in the For example, emulating a different "Host" header as sent in the
HTTP request from the browser to the server should be passed HTTP request from the browser to the server should be passed
as ``HTTP_HOST``. as ``HTTP_HOST``.
.. _CGI: http://www.w3.org/CGI/ .. _CGI: http://www.w3.org/CGI/
...@@ -1101,7 +1101,7 @@ TestCase ...@@ -1101,7 +1101,7 @@ TestCase
.. currentmodule:: django.test .. currentmodule:: django.test
Normal Python unit test classes extend a base class of ``unittest.TestCase``. Normal Python unit test classes extend a base class of ``unittest.TestCase``.
Django provides an extension of this base class: Django provides a few extensions of this base class:
.. class:: TestCase() .. class:: TestCase()
...@@ -1123,6 +1123,8 @@ additions, including: ...@@ -1123,6 +1123,8 @@ additions, including:
* Django-specific assertions for testing for things * Django-specific assertions for testing for things
like redirection and form errors. like redirection and form errors.
``TestCase`` inherits from :class:`~django.test.TransactionTestCase`.
.. class:: TransactionTestCase() .. class:: TransactionTestCase()
Django ``TestCase`` classes make use of database transaction facilities, if Django ``TestCase`` classes make use of database transaction facilities, if
...@@ -1153,6 +1155,7 @@ When running on a database that does not support rollback (e.g. MySQL with the ...@@ -1153,6 +1155,7 @@ When running on a database that does not support rollback (e.g. MySQL with the
MyISAM storage engine), ``TestCase`` falls back to initializing the database MyISAM storage engine), ``TestCase`` falls back to initializing the database
by truncating tables and reloading initial data. by truncating tables and reloading initial data.
``TransactionTestCase`` inherits from :class:`~django.test.SimpleTestCase`.
.. note:: .. note::
The ``TestCase`` use of rollback to un-do the effects of the test code The ``TestCase`` use of rollback to un-do the effects of the test code
...@@ -1166,6 +1169,31 @@ by truncating tables and reloading initial data. ...@@ -1166,6 +1169,31 @@ by truncating tables and reloading initial data.
A better long-term fix, that allows the test to take advantage of the A better long-term fix, that allows the test to take advantage of the
speed benefit of ``TestCase``, is to fix the underlying test problem. speed benefit of ``TestCase``, is to fix the underlying test problem.
.. class:: SimpleTestCase()
.. versionadded:: 1.4
A very thin subclass of :class:`unittest.TestCase`, it extends it with some
basic functionality like:
* Saving and restoring the Python warning machinery state.
* Checking that a callable :meth:`raises a certain exeception <TestCase.assertRaisesMessage>`.
If you need any of the other more complex and heavyweight Django-specific
features like:
* The ability to run tests with :ref:`modified settings <overriding-settings>`
* Using the :attr:`~TestCase.client` :class:`~django.test.client.Client`.
* Testing or using the ORM.
* Database :attr:`~TestCase.fixtures`.
* Custom test-time :attr:`URL maps <TestCase.urls>`.
* Test :ref:`skipping based on database backend features <skipping-tests>`.
* Our specialized :ref:`assert* <assertions>` metods.
then you should use :class:`~django.test.TransactionTestCase` or
:class:`~django.test.TestCase` instead.
``SimpleTestCase`` inherits from :class:`django.utils.unittest.TestCase`.
Default test client Default test client
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
...@@ -1370,7 +1398,7 @@ For example:: ...@@ -1370,7 +1398,7 @@ For example::
This test case will flush *all* the test databases before running This test case will flush *all* the test databases before running
``testIndexPageView``. ``testIndexPageView``.
.. _overriding-setting: .. _overriding-settings:
Overriding settings Overriding settings
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
...@@ -1402,7 +1430,9 @@ this use case Django provides a standard `Python context manager`_ ...@@ -1402,7 +1430,9 @@ this use case Django provides a standard `Python context manager`_
This example will override the :setting:`LOGIN_URL` setting for the code This example will override the :setting:`LOGIN_URL` setting for the code
in the ``with`` block and reset its value to the previous state afterwards. in the ``with`` block and reset its value to the previous state afterwards.
.. function:: utils.override_settings .. currentmodule:: django.test.utils
.. function:: override_settings
In case you want to override a setting for just one test method or even the In case you want to override a setting for just one test method or even the
whole TestCase class, Django provides the whole TestCase class, Django provides the
...@@ -1463,9 +1493,13 @@ contents of the test email outbox at the start of each test case. ...@@ -1463,9 +1493,13 @@ contents of the test email outbox at the start of each test case.
For more detail on email services during tests, see `Email services`_. For more detail on email services during tests, see `Email services`_.
.. _assertions:
Assertions Assertions
~~~~~~~~~~ ~~~~~~~~~~
.. currentmodule:: django.test
.. versionchanged:: 1.2 .. versionchanged:: 1.2
Addded ``msg_prefix`` argument. Addded ``msg_prefix`` argument.
...@@ -1474,11 +1508,19 @@ such as ``assertTrue`` and ``assertEqual``, Django's custom ``TestCase`` class ...@@ -1474,11 +1508,19 @@ such as ``assertTrue`` and ``assertEqual``, Django's custom ``TestCase`` class
provides a number of custom assertion methods that are useful for testing Web provides a number of custom assertion methods that are useful for testing Web
applications: applications:
The failure messages given by the assertion methods can be customized The failure messages given by most of these assertion methods can be customized
with the ``msg_prefix`` argument. This string will be prefixed to any with the ``msg_prefix`` argument. This string will be prefixed to any failure
failure message generated by the assertion. This allows you to provide message generated by the assertion. This allows you to provide additional
additional details that may help you to identify the location and details that may help you to identify the location and cause of an failure in
cause of an failure in your test suite. your test suite.
.. method:: TestCase.assertRaisesMessage(expected_exception, expected_message, callable_obj=None, *args, **kwargs)
Asserts that execution of callable ``callable_obj`` raised the
``expected_exception`` exception and that such exception has an
``expected_message`` representation. Any other outcome is reported as a
failure. Similar to unittest's ``assertRaisesRegexp`` with the difference
that ``expected_message`` isn't a regular expression.
.. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='') .. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
...@@ -1626,9 +1668,13 @@ manually, assign the empty list to ``mail.outbox``:: ...@@ -1626,9 +1668,13 @@ manually, assign the empty list to ``mail.outbox``::
# Empty the test outbox # Empty the test outbox
mail.outbox = [] mail.outbox = []
.. _skipping-tests:
Skipping tests Skipping tests
-------------- --------------
.. currentmodule:: django.test
.. versionadded:: 1.3 .. versionadded:: 1.3
The unittest library provides the ``@skipIf`` and ``@skipUnless`` The unittest library provides the ``@skipIf`` and ``@skipUnless``
...@@ -1651,8 +1697,7 @@ features class. See :class:`~django.db.backends.BaseDatabaseFeatures` ...@@ -1651,8 +1697,7 @@ features class. See :class:`~django.db.backends.BaseDatabaseFeatures`
class for a full list of database features that can be used as a basis class for a full list of database features that can be used as a basis
for skipping tests. for skipping tests.
skipIfDBFeature .. function:: skipIfDBFeature(feature_name_string)
~~~~~~~~~~~~~~~
Skip the decorated test if the named database feature is supported. Skip the decorated test if the named database feature is supported.
...@@ -1665,8 +1710,7 @@ it would under MySQL with MyISAM tables):: ...@@ -1665,8 +1710,7 @@ it would under MySQL with MyISAM tables)::
def test_transaction_behavior(self): def test_transaction_behavior(self):
# ... conditional test code # ... conditional test code
skipUnlessDBFeature .. function:: skipUnlessDBFeature(feature_name_string)
~~~~~~~~~~~~~~~~~~~
Skip the decorated test if the named database feature is *not* Skip the decorated test if the named database feature is *not*
supported. supported.
......
...@@ -19,12 +19,6 @@ class InvalidFields(admin.ModelAdmin): ...@@ -19,12 +19,6 @@ class InvalidFields(admin.ModelAdmin):
fields = ['spam'] fields = ['spam']
class ValidationTestCase(TestCase): class ValidationTestCase(TestCase):
def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
except Exception, e:
self.assertEqual(msg, str(e))
self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
def test_readonly_and_editable(self): def test_readonly_and_editable(self):
class SongAdmin(admin.ModelAdmin): class SongAdmin(admin.ModelAdmin):
......
...@@ -9,13 +9,6 @@ def pks(objects): ...@@ -9,13 +9,6 @@ def pks(objects):
class CustomColumnRegression(TestCase): class CustomColumnRegression(TestCase):
def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
except Exception, e:
self.assertEqual(msg, str(e))
self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
def setUp(self): def setUp(self):
self.a1 = Author.objects.create(first_name='John', last_name='Smith') self.a1 = Author.objects.create(first_name='John', last_name='Smith')
self.a2 = Author.objects.create(first_name='Peter', last_name='Jones') self.a2 = Author.objects.create(first_name='Peter', last_name='Jones')
......
...@@ -22,6 +22,7 @@ from django.core.files.base import ContentFile ...@@ -22,6 +22,7 @@ from django.core.files.base import ContentFile
from django.core.files.images import get_image_dimensions from django.core.files.images import get_image_dimensions
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.core.files.uploadedfile import UploadedFile from django.core.files.uploadedfile import UploadedFile
from django.test import SimpleTestCase
from django.utils import unittest from django.utils import unittest
# Try to import PIL in either of the two ways it can end up installed. # Try to import PIL in either of the two ways it can end up installed.
...@@ -36,14 +37,7 @@ except ImportError: ...@@ -36,14 +37,7 @@ except ImportError:
Image = None Image = None
class GetStorageClassTests(unittest.TestCase): class GetStorageClassTests(SimpleTestCase):
def assertRaisesErrorWithMessage(self, error, message, callable,
*args, **kwargs):
self.assertRaises(error, callable, *args, **kwargs)
try:
callable(*args, **kwargs)
except error, e:
self.assertEqual(message, str(e))
def test_get_filesystem_storage(self): def test_get_filesystem_storage(self):
""" """
...@@ -57,7 +51,7 @@ class GetStorageClassTests(unittest.TestCase): ...@@ -57,7 +51,7 @@ class GetStorageClassTests(unittest.TestCase):
""" """
get_storage_class raises an error if the requested import don't exist. get_storage_class raises an error if the requested import don't exist.
""" """
self.assertRaisesErrorWithMessage( self.assertRaisesMessage(
ImproperlyConfigured, ImproperlyConfigured,
"NonExistingStorage isn't a storage module.", "NonExistingStorage isn't a storage module.",
get_storage_class, get_storage_class,
...@@ -67,7 +61,7 @@ class GetStorageClassTests(unittest.TestCase): ...@@ -67,7 +61,7 @@ class GetStorageClassTests(unittest.TestCase):
""" """
get_storage_class raises an error if the requested class don't exist. get_storage_class raises an error if the requested class don't exist.
""" """
self.assertRaisesErrorWithMessage( self.assertRaisesMessage(
ImproperlyConfigured, ImproperlyConfigured,
'Storage module "django.core.files.storage" does not define a '\ 'Storage module "django.core.files.storage" does not define a '\
'"NonExistingStorage" class.', '"NonExistingStorage" class.',
......
...@@ -393,12 +393,6 @@ class TestFixtures(TestCase): ...@@ -393,12 +393,6 @@ class TestFixtures(TestCase):
class NaturalKeyFixtureTests(TestCase): class NaturalKeyFixtureTests(TestCase):
def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
except Exception, e:
self.assertEqual(msg, str(e))
self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
def test_nk_deserialize(self): def test_nk_deserialize(self):
""" """
......
...@@ -22,19 +22,6 @@ class BaseQuerysetTest(TestCase): ...@@ -22,19 +22,6 @@ class BaseQuerysetTest(TestCase):
def assertValueQuerysetEqual(self, qs, values): def assertValueQuerysetEqual(self, qs, values):
return self.assertQuerysetEqual(qs, values, transform=lambda x: x) return self.assertQuerysetEqual(qs, values, transform=lambda x: x)
def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
except Exception, e:
self.assertEqual(msg, str(e))
self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
else:
if hasattr(exc, '__name__'):
excName = exc.__name__
else:
excName = str(exc)
raise AssertionError("%s not raised" % excName)
class Queries1Tests(BaseQuerysetTest): class Queries1Tests(BaseQuerysetTest):
def setUp(self): def setUp(self):
......
from __future__ import with_statement from __future__ import with_statement
from django.test import TestCase, skipUnlessDBFeature from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
from django.utils.unittest import skip from django.utils.unittest import skip
from models import Person from models import Person
...@@ -48,6 +48,7 @@ class AssertNumQueriesTests(TestCase): ...@@ -48,6 +48,7 @@ class AssertNumQueriesTests(TestCase):
self.client.get("/test_utils/get_person/%s/" % person.pk) self.client.get("/test_utils/get_person/%s/" % person.pk)
self.assertNumQueries(2, test_func) self.assertNumQueries(2, test_func)
class AssertNumQueriesContextManagerTests(TestCase): class AssertNumQueriesContextManagerTests(TestCase):
urls = 'regressiontests.test_utils.urls' urls = 'regressiontests.test_utils.urls'
...@@ -129,6 +130,15 @@ class SkippingExtraTests(TestCase): ...@@ -129,6 +130,15 @@ class SkippingExtraTests(TestCase):
pass pass
class AssertRaisesMsgTest(SimpleTestCase):
def test_special_re_chars(self):
"""assertRaisesMessage shouldn't interpret RE special chars."""
def func1():
raise ValueError("[.*x+]y?")
self.assertRaisesMessage(ValueError, "[.*x+]y?", func1)
__test__ = {"API_TEST": r""" __test__ = {"API_TEST": r"""
# Some checks of the doctest output normalizer. # Some checks of the doctest output normalizer.
# Standard doctests do fairly # Standard doctests do fairly
......
...@@ -138,21 +138,13 @@ test_data = ( ...@@ -138,21 +138,13 @@ test_data = (
class NoURLPatternsTests(TestCase): class NoURLPatternsTests(TestCase):
urls = 'regressiontests.urlpatterns_reverse.no_urls' urls = 'regressiontests.urlpatterns_reverse.no_urls'
def assertRaisesErrorWithMessage(self, error, message, callable,
*args, **kwargs):
self.assertRaises(error, callable, *args, **kwargs)
try:
callable(*args, **kwargs)
except error, e:
self.assertEqual(message, str(e))
def test_no_urls_exception(self): def test_no_urls_exception(self):
""" """
RegexURLResolver should raise an exception when no urlpatterns exist. RegexURLResolver should raise an exception when no urlpatterns exist.
""" """
resolver = RegexURLResolver(r'^$', self.urls) resolver = RegexURLResolver(r'^$', self.urls)
self.assertRaisesErrorWithMessage(ImproperlyConfigured, self.assertRaisesMessage(ImproperlyConfigured,
"The included urlconf regressiontests.urlpatterns_reverse.no_urls "\ "The included urlconf regressiontests.urlpatterns_reverse.no_urls "\
"doesn't have any patterns in it", getattr, resolver, 'url_patterns') "doesn't have any patterns in it", getattr, resolver, 'url_patterns')
......
...@@ -4,22 +4,12 @@ Tests for stuff in django.utils.datastructures. ...@@ -4,22 +4,12 @@ Tests for stuff in django.utils.datastructures.
import copy import copy
import pickle import pickle
import unittest
from django.test import SimpleTestCase
from django.utils.datastructures import * from django.utils.datastructures import *
class DatastructuresTestCase(unittest.TestCase): class SortedDictTests(SimpleTestCase):
def assertRaisesErrorWithMessage(self, error, message, callable,
*args, **kwargs):
self.assertRaises(error, callable, *args, **kwargs)
try:
callable(*args, **kwargs)
except error, e:
self.assertEqual(message, str(e))
class SortedDictTests(DatastructuresTestCase):
def setUp(self): def setUp(self):
self.d1 = SortedDict() self.d1 = SortedDict()
self.d1[7] = 'seven' self.d1[7] = 'seven'
...@@ -125,7 +115,7 @@ class SortedDictTests(DatastructuresTestCase): ...@@ -125,7 +115,7 @@ class SortedDictTests(DatastructuresTestCase):
self.assertEqual(self.d1, {}) self.assertEqual(self.d1, {})
self.assertEqual(self.d1.keyOrder, []) self.assertEqual(self.d1.keyOrder, [])
class MergeDictTests(DatastructuresTestCase): class MergeDictTests(SimpleTestCase):
def test_simple_mergedict(self): def test_simple_mergedict(self):
d1 = {'chris':'cool', 'camri':'cute', 'cotton':'adorable', d1 = {'chris':'cool', 'camri':'cute', 'cotton':'adorable',
...@@ -179,7 +169,7 @@ class MergeDictTests(DatastructuresTestCase): ...@@ -179,7 +169,7 @@ class MergeDictTests(DatastructuresTestCase):
('key2', ['value2', 'value3']), ('key2', ['value2', 'value3']),
('key4', ['value5', 'value6'])]) ('key4', ['value5', 'value6'])])
class MultiValueDictTests(DatastructuresTestCase): class MultiValueDictTests(SimpleTestCase):
def test_multivaluedict(self): def test_multivaluedict(self):
d = MultiValueDict({'name': ['Adrian', 'Simon'], d = MultiValueDict({'name': ['Adrian', 'Simon'],
...@@ -198,7 +188,7 @@ class MultiValueDictTests(DatastructuresTestCase): ...@@ -198,7 +188,7 @@ class MultiValueDictTests(DatastructuresTestCase):
# MultiValueDictKeyError: "Key 'lastname' not found in # MultiValueDictKeyError: "Key 'lastname' not found in
# <MultiValueDict: {'position': ['Developer'], # <MultiValueDict: {'position': ['Developer'],
# 'name': ['Adrian', 'Simon']}>" # 'name': ['Adrian', 'Simon']}>"
self.assertRaisesErrorWithMessage(MultiValueDictKeyError, self.assertRaisesMessage(MultiValueDictKeyError,
'"Key \'lastname\' not found in <MultiValueDict: {\'position\':'\ '"Key \'lastname\' not found in <MultiValueDict: {\'position\':'\
' [\'Developer\'], \'name\': [\'Adrian\', \'Simon\']}>"', ' [\'Developer\'], \'name\': [\'Adrian\', \'Simon\']}>"',
d.__getitem__, 'lastname') d.__getitem__, 'lastname')
...@@ -248,7 +238,7 @@ class MultiValueDictTests(DatastructuresTestCase): ...@@ -248,7 +238,7 @@ class MultiValueDictTests(DatastructuresTestCase):
self.assertEqual({}, MultiValueDict().dict()) self.assertEqual({}, MultiValueDict().dict())
class DotExpandedDictTests(DatastructuresTestCase): class DotExpandedDictTests(SimpleTestCase):
def test_dotexpandeddict(self): def test_dotexpandeddict(self):
...@@ -262,13 +252,13 @@ class DotExpandedDictTests(DatastructuresTestCase): ...@@ -262,13 +252,13 @@ class DotExpandedDictTests(DatastructuresTestCase):
self.assertEqual(d['person']['2']['firstname'], ['Adrian']) self.assertEqual(d['person']['2']['firstname'], ['Adrian'])
class ImmutableListTests(DatastructuresTestCase): class ImmutableListTests(SimpleTestCase):
def test_sort(self): def test_sort(self):
d = ImmutableList(range(10)) d = ImmutableList(range(10))
# AttributeError: ImmutableList object is immutable. # AttributeError: ImmutableList object is immutable.
self.assertRaisesErrorWithMessage(AttributeError, self.assertRaisesMessage(AttributeError,
'ImmutableList object is immutable.', d.sort) 'ImmutableList object is immutable.', d.sort)
self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)') self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)')
...@@ -279,11 +269,11 @@ class ImmutableListTests(DatastructuresTestCase): ...@@ -279,11 +269,11 @@ class ImmutableListTests(DatastructuresTestCase):
self.assertEqual(d[1], 1) self.assertEqual(d[1], 1)
# AttributeError: Object is immutable! # AttributeError: Object is immutable!
self.assertRaisesErrorWithMessage(AttributeError, self.assertRaisesMessage(AttributeError,
'Object is immutable!', d.__setitem__, 1, 'test') 'Object is immutable!', d.__setitem__, 1, 'test')
class DictWrapperTests(DatastructuresTestCase): class DictWrapperTests(SimpleTestCase):
def test_dictwrapper(self): def test_dictwrapper(self):
f = lambda x: "*%s" % x f = lambda x: "*%s" % x
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment