#34180: Document that setting language in tests affects other tests -------------------------------------+------------------------------------- Reporter: Václav | Owner: nobody Řehák | Type: Bug | Status: new Component: | Version: 4.1 Documentation | Keywords: documentation i18n Severity: Normal | tests Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- The testing documentation https://docs.djangoproject.com/en/4.1/topics/testing/tools/#setting-the- language suggests to set language in tests using a cookie or http header. But it is not obvious to the reader that doing so will switch the Django active language for all other subsequent tests.
In our case we had random test failures in parallel run depending on the actual order of tests executed. This is easily reproducible with the following test file {{{ from django.contrib.auth.forms import AuthenticationForm from django.test import TestCase class ViewTestCase(TestCase): def test_czech_request(self): self.client.get("/", HTTP_ACCEPT_LANGUAGE="cs-cz") class FormTestCase(TestCase): def test_form(self): f = AuthenticationForm(data={}) self.assertEqual(f.errors['username'], ['This field is required.']) }}} This testsuite passes in normal run (form is tested before the view) but fails when running tests with --reverse as the view test switches Django to Czech language and the validation errors of the form are translated. I'm not sure if this can be fixed (should the test runner activate the default language before each test?) but I suggest to at least document it and probably recommend the user to activate the default language in tearDown method. {{{ def tearDown(self): translation.activate(settings.LANGUAGE_CODE) }}} -- Ticket URL: <https://code.djangoproject.com/ticket/34180> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/01070184a930fd62-399ebde2-91fa-4f6d-bb6f-3f627e94e00d-000000%40eu-central-1.amazonses.com.