Opened 3 years ago
Last modified 22 months ago
#32939 assigned Cleanup/optimization
Change override_settings to do its subclass check lazily — at Initial Version
Reported by: | Chris Jerdonek | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Florian Demmer | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently, the @override_settings
decorator raises a ValueError
if it's used to decorate a class that doesn't inherit from SimpleTestCase
:
https://github.com/django/django/blob/56f9579105c324ff15250423bf9f8bdf1634cfb4/django/test/utils.py#L519-L521
However, this prevents the decorator from being useable as a test-class mixin for a number of SimpleTestCase
subclasses. When creating shared test-class functionality, it's often better to use a mixin rather than a concrete TestCase
class. This way the mixin won't have its setUp
methods run unnecessarily on a test-case class with no tests, and it won't count towards the parallel test runner's test-case class count, etc.
The check could instead be done lazily, e.g. inside setUpClass()
, while still having the same desired effect.
I noticed this when seeing if I could convert AuthViewsTestCase
into a mixin:
https://github.com/django/django/blob/56f9579105c324ff15250423bf9f8bdf1634cfb4/tests/auth_tests/test_views.py
The AuthViewsTestCase
change could be done in a nicer way once this ticket is implemented, perhaps even as part of this ticket.