【发布时间】:2011-12-04 08:46:57
【问题描述】:
我尝试在 Django 单元测试中使用 utf8 字符串并包含
# -*- coding: utf-8 -*-
但是 django-admin.py 仍然抱怨没有编码。
Traceback(最近一次调用最后一次):
文件“/home/basti/work/virtualenv/bin/django-admin.py”,第 5 行,在 management.execute_from_command_line()
文件 "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py", 第 429 行,在 execute_from_command_line 实用程序.execute()
文件 "/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py", 第 379 行,执行中 self.fetch_command(subcommand).run_from_argv(self.argv)
文件 "> /home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py", 第 191 行,在 run_from_argv self.execute(*args, **options.dict)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py”, 第 220 行,在执行中 输出 = self.handle(*args, **options)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/south/management/commands/test.py”, 第 8 行,在句柄中 super(Command, self).handle(*args, **kwargs)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/commands/test.py”, 第 37 行,在句柄中 失败 = test_runner.run_tests(test_labels)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py”, 第 358 行,在 run_tests 套件 = self.build_suite(test_labels, extra_tests)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py”, 第 248 行,在 build_suite 中 suite.addTest(build_suite(app))
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py”, 第 77 行,在 build_suite 中 test_module = get_tests(app_module)
文件 “/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py”, 第 35 行,在 get_tests 中 test_module = import('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
SyntaxError: 第 242 行的文件 tests.py 中的非 ASCII 字符“\xc3”, 但没有声明编码;见http://www.python.org/peps/pep-0263.html 了解详情
代码是
# -- 编码: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)
我尝试将 TEST_CHARSET 和 TEST_DATABASE_CHARSET 设置为 utf8,但仍然没有成功。
关于如何解决这个问题的任何提示?
TIA && 祝你有美好的一天!
巴斯蒂
【问题讨论】:
-
你不想显示第 242 个字符串吗?
标签: django unit-testing