Commit 9da81ada by Will Daly

Merge pull request #5626 from edx/will/more-unit-tests-failures

Fix unit test failures due to duplicate url names
parents 6250c95d d558a361
......@@ -30,8 +30,10 @@ class TestCase(unittest.TestCase):
def setUp(self):
super(TestCase, self).setUp()
self._original_providers = provider.Registry._get_all()
provider.Registry._reset()
def tearDown(self):
provider.Registry._reset()
provider.Registry.configure_once(self._original_providers)
super(TestCase, self).tearDown()
......@@ -19,19 +19,38 @@ class UrlResetMixin(object):
that affect the contents of urls.py
"""
def _reset_urls(self, urlconf=None):
if urlconf is None:
urlconf = settings.ROOT_URLCONF
if urlconf in sys.modules:
reload(sys.modules[urlconf])
def _reset_urls(self, urlconf_modules):
for urlconf in urlconf_modules:
if urlconf in sys.modules:
reload(sys.modules[urlconf])
clear_url_caches()
# Resolve a URL so that the new urlconf gets loaded
resolve('/')
def setUp(self, **kwargs):
"""Reset django default urlconf before tests and after tests"""
def setUp(self, *args, **kwargs):
"""Reset Django urls before tests and after tests
If you need to reset `urls.py` from a particular Django app (or apps),
specify these modules in *args.
Examples:
# Reload only the root urls.py
super(MyTestCase, self).setUp()
# Reload urls from my_app
super(MyTestCase, self).setUp("my_app.urls")
# Reload urls from my_app and another_app
super(MyTestCase, self).setUp("my_app.urls", "another_app.urls")
"""
super(UrlResetMixin, self).setUp(**kwargs)
self._reset_urls()
self.addCleanup(self._reset_urls)
urlconf_modules = [settings.ROOT_URLCONF]
if args:
urlconf_modules.extend(args)
self._reset_urls(urlconf_modules)
self.addCleanup(lambda: self._reset_urls(urlconf_modules))
......@@ -47,7 +47,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
@patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True})
def setUp(self):
super(StudentAccountViewTest, self).setUp()
super(StudentAccountViewTest, self).setUp("student_account.urls")
# Create/activate a new account
activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.OLD_EMAIL)
......@@ -62,8 +62,8 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
self.assertContains(response, "Student Account")
@ddt.data(
("login", "login"),
("register", "register"),
("account_login", "login"),
("account_register", "register"),
)
@ddt.unpack
def test_login_and_registration_form(self, url_name, initial_mode):
......@@ -71,7 +71,7 @@ class StudentAccountViewTest(UrlResetMixin, TestCase):
expected_data = u"data-initial-mode=\"{mode}\"".format(mode=initial_mode)
self.assertContains(response, expected_data)
@ddt.data("login", "register")
@ddt.data("account_login", "account_register")
def test_login_and_registration_third_party_auth_urls(self, url_name):
response = self.client.get(reverse(url_name))
......
......@@ -4,8 +4,8 @@ from django.conf import settings
urlpatterns = patterns(
'student_account.views',
url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='login'),
url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='register'),
url(r'^login/$', 'login_and_registration_form', {'initial_mode': 'login'}, name='account_login'),
url(r'^register/$', 'login_and_registration_form', {'initial_mode': 'register'}, name='account_register'),
)
if settings.FEATURES.get('ENABLE_NEW_DASHBOARD'):
......
......@@ -35,7 +35,7 @@ class StudentProfileViewTest(UrlResetMixin, TestCase):
@patch.dict(settings.FEATURES, {'ENABLE_NEW_DASHBOARD': True})
def setUp(self):
super(StudentProfileViewTest, self).setUp()
super(StudentProfileViewTest, self).setUp("student_profile.urls")
# Create/activate a new account
activation_key = account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL)
......
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