1
votes

I try to use a utf8 string in a Django unit test and have included

# -*- coding: utf-8 -*-

but django-admin.py still complaints there is no encoding.

Traceback (most recent call last):

File "/home/basti/work/virtualenv/bin/django-admin.py", line 5, in management.execute_from_command_line()

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py", line 429, in execute_from_command_line utility.execute()

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "> /home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv self.execute(*args, **options.dict)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute output = self.handle(*args, **options)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/south/management/commands/test.py", line 8, in handle super(Command, self).handle(*args, **kwargs)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 37, in handle failures = test_runner.run_tests(test_labels)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py", line 358, in run_tests suite = self.build_suite(test_labels, extra_tests)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py", line 248, in build_suite suite.addTest(build_suite(app))

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py", line 77, in build_suite test_module = get_tests(app_module)

File "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py", line 35, in get_tests test_module = import('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)

SyntaxError: Non-ASCII character '\xc3' in file tests.py on line 242, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

The code is

# -- coding: utf-8 --

"""                                                                                                                                                                                                         
This file demonstrates writing tests using the unittest module. These will pass                                                                                                                             
when you run "manage.py test".                                                                                                                                                                              

Replace this with more appropriate tests for your application.                                                                                                                                              
"""

from django.test import TestCase, Client
from django.contrib.auth.models import User
from django.db.utils import IntegrityError
from django.core.urlresolvers import reverse
from django.utils.encoding import smart_unicode

class ViewTests(TestCase):
    def setUp(self):
    self.client = Client()

   def test_french(self):
    self.client.cookies["django_language"] = 'fr'
    r = self.client.get("/")
    self.assertTrue(smart_unicode(u"Se déconnecter") in r.content)

I've tried to set TEST_CHARSET and TEST_DATABASE_CHARSET to utf8, but still no luck.

Any hints on how to solve that?

TIA && have a nice day!

Basti

4
Don't you want to show the 242nd string?DrTyrsa

4 Answers

3
votes

Put:

# -*- coding: utf-8 -*-

As the first line for that file.

2
votes

To answer my own question the problem wasn't the matching string but the encoding of the requests content! Changing the test as followed fix it

self.assertTrue(u"Se déconnecter" in r.content.decode('utf8'))

0
votes

Ensure you have the encoding defined in each file including models and of course in tests.py. If possible, get the latest version of django.

If the administrator is still not working, try:

Solution #1:

from django.utils.encoding import smart_unicode

On the model definition:

def __unicode__(self):  
    return smart_unicode(("%s" % self.field))

Solution #2: Literal method

def __unicode__(self):   
    return (u"%s" % self.field)  

Always set

DEFAULT_CHARSET='utf-8'  

in settings.py

You can also translate [this post][1] [1]: http://www.pvilas.com/2011/03/django-evitar-error-en-el-administrador.html from spanish. I hope can help.

0
votes

I had this problem, because in my models.py encoding was not set for the file. Putting # -*- coding: utf-8 -*- in my models.py and having the same in my test files solved the problem.