我使用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 –
仅供参考,您的答案不起作用,由于我在我自己的答案中解释的原因。不错,尽管如此,但它根本行不通。 –