How to disable translations during unit tests in django?

Posted by Denilson Sá on Stack Overflow See other posts from Stack Overflow or by Denilson Sá
Published on 2012-05-31T18:44:27Z Indexed on 2012/06/04 16:41 UTC
Read the original article Hit count: 196

Filed under:
|
|

I'm using Django Internationalization tools to translate some strings from my application. The code looks like this:

from django.utils.translation import ugettext as _
def my_view(request):
    output = _("Welcome to my site.")
    return HttpResponse(output)

Then, I'm writing unit tests using the Django test client. These tests make a request to the view and compare the returned contents.

How can I disable the translations while running the unit tests? I'm aiming to do this:

class FoobarTestCase(unittest.TestCase):
    def setUp(self):
        # Do something here to disable the string translation. But what?
        # I've already tried this, but it didn't work:
        django.utils.translation.deactivate_all()
    def testFoobar(self):
        c = Client()
        response = c.get("/foobar")
        # I want to compare to the original string without translations.
        self.assertEquals(response.content.strip(), "Welcome to my site.")

© Stack Overflow or respective owner

Related posts about python

Related posts about django