Sto usando gli strumenti Django Internationalization per tradurre alcune stringhe dalla mia applicazione. Il codice è simile al seguente:Come disabilitare le traduzioni durante i test unitari in django?
from django.utils.translation import ugettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)
Poi, sto scrivendo unit test utilizzando il Django test client. Questi test fanno una richiesta alla vista e confrontano i contenuti restituiti.
Come faccio a disattivare le traduzioni durante l'esecuzione dei test di unità? Ho intenzione di fare questo:
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.")
Potrebbe essere una buona idea, avete suggerimenti su come raggiungerlo? –
Prova questo: http://stackoverflow.com/a/9843158/15369 –
FYI, la tua risposta non funziona, per i motivi che ho spiegato nella mia risposta. Bel tentativo, però, ma semplicemente non funziona. –