Code

Opened 14 months ago

Last modified 2 months ago

#19869 assigned New feature

Make changing the active language inside `LiveTestServerCase` possible

Reported by: void Owned by: unaizalakain
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: apollo13 Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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):
        self.selenium.get(something)
        ...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 (13)

comment:1 Changed 14 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 14 months ago by void

Selenium does not let you set custom request headers: http://code.google.com/p/selenium/issues/detail?id=141.
I am actually trying to test django admin, it have no such "change language" feature.

comment:3 Changed 14 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 14 months ago by julien

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

Version 0, edited 14 months ago by julien (next)

comment:5 Changed 5 months ago by unaizalakain

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

comment:6 Changed 5 months ago by void

With some kind of message or signal, I think

comment:7 Changed 5 months ago by unaizalakain

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

comment:8 Changed 5 months ago by unaizalakain

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

comment:9 Changed 5 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 5 months ago by unaizalakain

  • Has patch set

comment:11 Changed 5 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 5 months ago by unaizalakain (previous) (diff)

comment:12 Changed 2 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 2 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from unaizalakain 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'
Author


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

 
Note: See TracTickets for help on using tickets.