﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34180	Document that setting language in tests affects other tests	Václav Řehák	Faris Naimi	"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)
}}}

"	Bug	closed	Documentation	4.1	Normal	fixed	documentation i18n tests		Ready for checkin	1	0	0	0	0	0
