我正在使用Django国际化工具翻译我的应用程序中的一些字符串。代码如下:
from django.utils.translation import ugettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)
接下来,我将使用Django测试客户端编写单元测试。这些测试向视图发出请求并比较返回的内容。
我如何在运行单元测试时禁用翻译?我的目标是这样做:
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.")