Opened 2 years ago

Closed 2 years ago

Last modified 11 days ago

#21977 closed Cleanup/optimization (fixed)

Deprecate SimpleTestCase.urls in favor of override_settings

Reported by: timo Owned by: anubhav9042
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: anubhav9042@…, loic@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


As summarized by Aymeric in #21518:

Clearly we'd better provide just "one way to [override ROOT_URLCONF" in the long term. We could consider the "urls" attribute as an old and specific way to override a particular setting. That would give us a reason deprecate it in favor of the standard settings override mechanisms, @override_settings(...) and with self.settings(...):.

Change History (15)

comment:1 Changed 2 years ago by anubhav9042

  • Cc anubhav9042@… added

Which one way we should be going towards?

comment:2 Changed 2 years ago by timo

We should see if it's feasible to replace SimpleTestCase.urls = 'foo' (and deprecate this syntax) with override_settings(ROOT_URLCONF='foo').

comment:3 Changed 2 years ago by anubhav9042

  • Cc anubhav9042@… removed
  • Owner changed from nobody to anubhav9042
  • Status changed from new to assigned

comment:4 Changed 2 years ago by anubhav9042

  • Cc anubhav9042@… added

comment:5 Changed 2 years ago by anubhav9042

Well I have tried replacing it at some places.
Is something like this: required?
Also do we need to remove the SimplaeTestCase.urls feature completely or just change usage??

comment:6 Changed 2 years ago by timo

SimpleTestCase.urls will need to raise a warning as described in Deprecating a feature. You'll need to wait until we cut the 1.7 branch as the deprecation will need to start in 1.8.

comment:7 Changed 2 years ago by anubhav9042

Ok I will work on the patch after 1.7 branch is cut out and new master is there.

comment:8 Changed 2 years ago by anubhav9042

I have some doubts regarding this.

  • Should the function which sets .urls should be changed or we are just changing its usage everywhere in the tests.
  • Do I need to write a test for this?

comment:9 Changed 2 years ago by timo

Yes, the function which sets urls should warn with a RemovedInDjango20Warning (two releases from now) so that users who are using this feature are warned to update their code. The deprecation message should say something like "SimpleTestCase.urls is deprecated, use @override_settings(ROOT_URLCONF=...) instead.".

A test to verify that the warning is raised under the appropriate circumstances wouldn't hurt.

comment:10 Changed 2 years ago by loic84

  • Cc loic@… added

I did a quick run with -Wall and found a couple of SimpleTestCase.urls that needed updating. Running the test suite with --selenium found a couple more, so there may be more skipped tests that need updating. I guess the logs of the CI will have to be monitored on the various setups.

The stacklevel for this warning isn't helpful at all, I've tried levels up to 6 with no luck, so the faulty TestCase really need to be specified in the warning message. For some reason -Werror doesn't do anything here, so the warning message is really the only indication one has to find the guilty TestCase.

Testing the deprecation is not that easy, I came up with:

class DeprecatingSimpleTestCaseUrls(unittest.TestCase):
    def test_deprecation(self):
        class TempTestCase(SimpleTestCase):
            urls = 'tests.urls'

            def test(self):

        with warnings.catch_warnings(record=True) as recorded:
            suite = unittest.TestLoader().loadTestsFromTestCase(TempTestCase)
            with open(os.devnull, 'w') as devnull:
                unittest.TextTestRunner(stream=devnull, verbosity=2).run(suite)

        # FIXME: Check that the correct warning is raised.

comment:11 Changed 2 years ago by anubhav9042

  • Has patch set


Thanks Loic for help in testing and reviewing.

comment:12 Changed 2 years ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In cd914e31c9a889f18c50c15b4f6ee4959624001f:

Fixed #21977 -- Deprecated SimpleTestCase.urls

comment:13 Changed 9 months ago by Tim Graham <timograham@…>

In 1392aff:

Refs #21977 -- Removed SimpleTestCase.urls per deprecation timeline.

comment:14 Changed 11 days ago by lanzz

Performing any request while @override.settings(ROOT_URLCONF) is in effect stores that override in the django.urls.base._urlconfs thread local and nothing takes care to clean it up afterwards. This leads to leaking — once ROOT_URLCONF is overridden, it remains in effect even after the override goes out of scope and applies to all subsequent tests that try to resolve URLs before they perform a request (which is what would usually happen).

Last edited 11 days ago by lanzz (previous) (diff)

comment:15 Changed 11 days ago by timgraham

Please open a new ticket with a failing test for Django's test suite or steps to reproduce, thanks.

Note: See TracTickets for help on using tickets.
Back to Top