Opened 9 days ago

Last modified 4 days ago

#35986 new Bug

Test classes with @translation.override decorator are not run

Reported by: Arthur Pemberton Owned by:
Component: Utilities Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For example, the test "CustomTimeInputFormatsTests" does not get discovered and ran via:
./runtests.py forms_tests.tests.test_input_formats

And attempting to run the test direct with ./runtests.py forms_tests.tests.test_input_formats.CustomTimeInputFormatsTests results in an error:

AttributeError: 'CustomTimeInputFormatsTests' object has no attribute 'runTest'. Did you mean: 'subTest'?

Using Python 3.12, I followed the steps from https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/#running-the-unit-tests to setup a clean environment.

Attachments (1)

test_input_formats-output.txt (5.0 KB ) - added by Arthur Pemberton 9 days ago.

Download all attachments as: .zip

Change History (8)

by Arthur Pemberton, 9 days ago

comment:1 by Arthur Pemberton, 9 days ago

From that module, the following tests are seemingly all affected:

  • CustomTimeInputFormatsTests
  • CustomDateInputFormatsTests
  • CustomDateTimeInputFormatsTests

comment:2 by Sarah Boyce, 9 days ago

Component: UncategorizedUtilities
Summary: Some unit tests in forms_tests.tests.test_input_formats not runningTest classes with @translation.override decorator are not run
Triage Stage: UnreviewedAccepted

Thank you - well spotted!
Looks like any test class with a class level @translation.override is affected (this includes tests.timezones.tests.TemplateTests and tests.timezones.tests.AdminTests)

comment:3 by Arthur Pemberton, 9 days ago

I believe this bug is covering the existence of another bug: that TIME_INPUT_FORMATS is not used by forms.TimeField as one might expect -- and that in fact, CustomTimeInputFormatsTests, which tests this, should be failing.

Should I wait on this ticket before creating one about TIME_INPUT_FORMATS not working as expected? Or should I create another ticket in parallel?

comment:4 by Jacob Walls, 9 days ago

I think separate tickets make sense.

comment:5 by Simon Charette, 4 days ago

Tests appears to pass even with the timezone.override adjustments

  • tests/forms_tests/tests/test_input_formats.py

    diff --git a/tests/forms_tests/tests/test_input_formats.py b/tests/forms_tests/tests/test_input_formats.py
    index c5023d8d10..30ca53ba4b 100644
    a b def test_localized_timeField_with_inputformat(self):  
    118118        self.assertEqual(text, "13:30:00")
    119119
    120120
    121 @translation.override(None)
    122121@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"])
    123122class CustomTimeInputFormatsTests(SimpleTestCase):
     123    @classmethod
     124    def setUpClass(cls):
     125        cls.enterClassContext(translation.override(None))
     126        super().setUpClass()
     127
    124128    def test_timeField(self):
    125129        "TimeFields can parse dates in the default format"
    126130        f = forms.TimeField()
    def test_localized_dateField_with_inputformat(self):  
    431435        self.assertEqual(text, "21.12.2010")
    432436
    433437
    434 @translation.override(None)
    435438@override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"])
    436439class CustomDateInputFormatsTests(SimpleTestCase):
     440    @classmethod
     441    def setUpClass(cls):
     442        cls.enterClassContext(translation.override(None))
     443        super().setUpClass()
     444
    437445    def test_dateField(self):
    438446        "DateFields can parse dates in the default format"
    439447        f = forms.DateField()
    def test_localized_dateTimeField_with_inputformat(self):  
    752760        self.assertEqual(text, "21.12.2010 13:30:00")
    753761
    754762
    755 @translation.override(None)
    756763@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"])
    757764class CustomDateTimeInputFormatsTests(SimpleTestCase):
     765    @classmethod
     766    def setUpClass(cls):
     767        cls.enterClassContext(translation.override(None))
     768        super().setUpClass()
     769
    758770    def test_dateTimeField(self):
    759771        "DateTimeFields can parse dates in the default format"
    760772        f = forms.DateTimeField()
  • tests/timezones/tests.py

    diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py
    index c45f078ef6..c66c9a9c75 100644
    a b def test_aware_datetime_in_other_timezone(self):  
    921921                self.assertEqual(obj.dt, dt)
    922922
    923923
    924 @translation.override(None)
    925924@override_settings(DATETIME_FORMAT="c", TIME_ZONE="Africa/Nairobi", USE_TZ=True)
    926925class TemplateTests(SimpleTestCase):
     926    @classmethod
     927    def setUpClass(cls):
     928        cls.enterClassContext(translation.override(None))
     929        super().setUpClass()
     930
    927931    @requires_tz_support
    928932    def test_localtime_templatetag_and_filters(self):
    929933        """
    def test_localized_model_form(self):  
    13241328            self.assertIn("2011-09-01 17:20:30", str(form))
    13251329
    13261330
    1327 @translation.override(None)
    13281331@override_settings(
    13291332    DATETIME_FORMAT="c",
    13301333    TIME_ZONE="Africa/Nairobi",
    def test_localized_model_form(self):  
    13341337class AdminTests(TestCase):
    13351338    @classmethod
    13361339    def setUpTestData(cls):
     1340        cls.enterClassContext(translation.override(None))
    13371341        cls.u1 = User.objects.create_user(
    13381342            password="secret",
    13391343            last_login=datetime.datetime(2007, 5, 30, 13, 20, 10, tzinfo=UTC),
    def test_change_readonly_in_other_timezone(self):  
    14031407                reverse("admin_tz:timezones_timestamp_change", args=(t.pk,))
    14041408            )
    14051409        self.assertContains(response, t.created.astimezone(ICT).isoformat())
     1410        self.assertContains(response, t.created.astimezone(ICT).isoformat())
Version 0, edited 4 days ago by Simon Charette (next)

comment:6 by Jacob Walls, 4 days ago

For reference, this is the companion ticket that was closed: ticket:35990.

comment:7 by Arthur Pemberton, 4 days ago

Correct. This seems to work as such by design.

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