我使用Django Internationalization工具來從我的應用程序中翻譯一些字符串。代碼如下所示:如何在django的單元測試期間禁用翻譯?
from django.utils.translation import ugettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)
然後,我正在寫使用Django test client單元測試。這些測試向視圖發出請求並比較返回的內容。
如何在運行單元測試時禁用翻譯?我的目標要做到這一點:
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.")
這可能是一個好主意,你有如何實現這一目標的建議? –
試試這個:http://stackoverflow.com/a/9843158/15369 –
僅供參考,您的答案不起作用,由於我在我自己的答案中解釋的原因。不錯,儘管如此,但它根本行不通。 –