How to disable translations during unit tests in django?
- by Denilson Sá
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.")