Opened 20 months ago

Closed 18 months ago

Last modified 2 weeks 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 (13)

comment:1 Changed 19 months ago by anubhav9042

  • Cc anubhav9042@… added

Which one way we should be going towards?

comment:2 Changed 19 months 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 19 months ago by anubhav9042

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

comment:4 Changed 19 months ago by anubhav9042

  • Cc anubhav9042@… added

comment:5 Changed 19 months 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 19 months 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 19 months 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 19 months 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 19 months 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 18 months 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 18 months ago by anubhav9042

  • Has patch set


Thanks Loic for help in testing and reviewing.

comment:12 Changed 18 months 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 2 weeks ago by Tim Graham <timograham@…>

In 1392aff:

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

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