Opened 17 months ago

Last modified 5 weeks ago

#19869 assigned New feature

Make changing the active language inside `LiveTestServerCase` possible

Reported by: void Owned by: gchp
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: apollo13 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.

Attachments (0)

Change History (16)

comment:1 Changed 17 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 17 months ago by void

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 17 months ago by julien

  • Triage Stage changed from Unreviewed to Accepted

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 17 months ago by julien

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

Last edited 17 months ago by julien (previous) (diff)

comment:5 Changed 8 months ago by unaizalakain

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

comment:6 Changed 8 months ago by void

With some kind of message or signal, I think

comment:7 Changed 8 months ago by unaizalakain

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

comment:8 Changed 7 months ago by unaizalakain

  • Owner changed from nobody to unaizalakain
  • Status changed from new to assigned

comment:9 Changed 7 months ago by unaizalakain

  • Summary changed from Add ability to restart LiveServerThread inside a test method to Make changing the active language inside `LiveTestServerCase` possible

comment:10 Changed 7 months ago by unaizalakain

  • Has patch set

comment:11 Changed 7 months ago by unaizalakain

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 7 months ago by unaizalakain (previous) (diff)

comment:12 Changed 5 months ago by apollo13

  • Cc apollo13 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 months ago by unaizalakain

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 weeks ago by timo

  • Patch needs improvement set

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

comment:15 Changed 5 weeks ago by unaizalakain

  • Has patch unset
  • Owner unaizalakain deleted
  • Patch needs improvement unset
  • Status changed from assigned to new

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 weeks ago by gchp

  • Owner set to gchp
  • Status changed from new to assigned

Add Comment

Modify Ticket

Change Properties
<Author field>
as assigned
The owner will be changed from gchp to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.