Opened 6 years ago

Closed 4 years ago

#19869 closed New feature (wontfix)

Make changing the active language inside `LiveTestServerCase` possible

Reported by: Alexey Boriskin Owned by: Greg Chapple
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: Florian Apolloner Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I was trying to make a selenium test to work with different locales in one test, like this:

for locale in locale_list:
    with translation.override(locale):
        ...test something...

But translation.override had affected only current thread, not LiveServerThread, probably because active translation is thread-local. Because I see no way to force LiveServerThread copy or share same thread-local state, it would be cool if I could have ability to stop LiveServerThread and recreate it in such cases. Currently it is started/stopped in setUpClass/tearDownClass which means that it is created once per TestCase run and shared between all the runs of all the test methods.

Change History (18)

comment:1 Changed 6 years ago by Aymeric Augustin

Can you set the Accept-Language header in the request, or use your website's "change language" feature?

comment:2 Changed 6 years ago by Alexey Boriskin

Selenium does not let you set custom request headers:
I am actually trying to test django admin, it have no such "change language" feature.

comment:3 Changed 6 years ago by Julien Phalip

Triage Stage: UnreviewedAccepted

I think one good way of doing this is to add a new method "activate_live_server_locale(self, locale)" to LiveServerTestCase that would activate the locale in the separate thread. It would be even better if it could work as a context manager like so:

class MyTests(LiveServerTestcase):

    def test_something(self):
        with self.activate_live_server_locale('de'):

comment:4 Changed 6 years ago by Julien Phalip

By the way, "activate_live_server_locale" is a bit of a mouthful, so suggestions for better names are welcome ;)

Last edited 6 years ago by Julien Phalip (previous) (diff)

comment:5 Changed 5 years ago by Unai Zalakain

How would you modify the thread local of the live server thread from the main thread?

comment:6 Changed 5 years ago by Alexey Boriskin

With some kind of message or signal, I think

comment:7 Changed 5 years ago by Unai Zalakain

I believe (though I may be wrong) that signals don't work across threads.

comment:8 Changed 5 years ago by Unai Zalakain

Owner: changed from nobody to Unai Zalakain
Status: newassigned

comment:9 Changed 5 years ago by Unai Zalakain

Summary: Add ability to restart LiveServerThread inside a test methodMake changing the active language inside `LiveTestServerCase` possible

comment:10 Changed 5 years ago by Unai Zalakain

Has patch: set

comment:11 Changed 5 years ago by Unai Zalakain

The set_language view could be used to change the live test server thread language but that would mean adding some kind of new method to the test case and the inability to use the already defined methods (django.utils.translation.override, django.utils.translation.activate, etc).

Instead, the PR opts for making the thread local that holds the current active language a non-local. This is done in the two threads so suddenly one thread is capable of accessing the other one's active language. This is afterwards reverted to thread locals again.

Last edited 5 years ago by Unai Zalakain (previous) (diff)

comment:12 Changed 5 years ago by Florian Apolloner

Cc: Florian Apolloner added

I'd very much prefer a method which just sets the active language in the other thread as suggested by julien. While the PR might work, it makes me run away in fear.

comment:13 Changed 5 years ago by Unai Zalakain

Well, it surely isn't the most elegant way of doing it but defining some special functions for changing the active language when those functions already exist doesn't seem right to me either.

I think disabling the language thread-local in the testing environment isn't too bad, after all, it's not needed.

comment:14 Changed 5 years ago by Tim Graham

Patch needs improvement: set

If the patch makes apollo13 run, I guess it needs improvement. :-)

comment:15 Changed 5 years ago by Unai Zalakain

Has patch: unset
Owner: Unai Zalakain deleted
Patch needs improvement: unset
Status: assignednew

Then more than an improvement, I think a totally different patch is needed, although I don't understand the need for a language thread-local in a testing environment that doesn't support multithreading.

comment:16 Changed 5 years ago by Greg Chapple

Owner: set to Greg Chapple
Status: newassigned

comment:17 Changed 4 years ago by Alexey Boriskin

The initial goal of this ticket was to deal with issue that emerged while working on the #18767.
That is, translation.override was not able to change thread-local state of LiveServerThread, and it rendered everything in one language.
#18767 ticket is fixed now and thread-local issue resolved by introducing settings_changed signal and its receivers, in particular to the trans_real._active = threading.local() line in django.test.signals.

Therefore, I think it's safe to close this ticket as wontfix.

comment:18 Changed 4 years ago by Alexey Boriskin

Resolution: wontfix
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top