James Bennett avatar James Bennett committed 0743cf0

Factor out common logic in the backend-retrieval tests.

Comments (0)

Files changed (1)

registration/tests/backends.py

 from django.core.exceptions import ImproperlyConfigured
 from django.test import TestCase
 
+from registration import get_backend
 from registration import forms
 from registration import signals
+from registration.backends.default import DefaultBackend
 from registration.models import RegistrationProfile
 
 
     properly.
 
     """
+    def setUp(self):
+        """
+        Stash away the original value of
+        ``settings.REGISTRATION_BACKEND`` so it can be restored later.
+        
+        """
+        self.old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
+
+    def tearDown(self):
+        """
+        Restore the value of ``settings.REGISTRATION_BACKEND``.
+        
+        """
+        settings.REGISTRATION_BACKEND = self.old_backend
+    
     def test_get_backend(self):
         """
         Set ``REGISTRATION_BACKEND`` temporarily, then verify that
         ``get_backend()`` returns the correct value.
 
         """
-        from registration import get_backend
-        from registration.backends.default import DefaultBackend
-
-        old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
-
         settings.REGISTRATION_BACKEND = 'registration.backends.default.DefaultBackend'
         self.failUnless(isinstance(get_backend(), DefaultBackend))
 
-        settings.REGISTRATION_BACKEND = old_backend
-
     def test_backend_error_none(self):
         """
         Test that an invalid value for the ``REGISTRATION_BACKEND``
         setting raises the correct exception.
 
         """
-        from registration import get_backend
-
-        old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
-
         settings.REGISTRATION_BACKEND = None
         self.assertRaises(ImproperlyConfigured, get_backend)
 
-        settings.REGISTRATION_BACKEND = old_backend
-
     def test_backend_error_invalid(self):
         """
         Test that a nonexistent/unimportable backend raises the
         correct exception.
 
         """
-        from registration import get_backend
-
-        old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
-
         settings.REGISTRATION_BACKEND = 'registration.backends.doesnotexist.NonExistentBackend'
         self.assertRaises(ImproperlyConfigured, get_backend)
 
-        settings.REGISTRATION_BACKEND = old_backend
-
 
 class DefaultRegistrationBackendTests(TestCase):
     """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.