Unicode in Django unit test
Question: 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


Answer: Put:

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

As the first line for that file.

