Django测试:TypeError:期望unicode参数,得到' str'

时间:2014-09-25 14:05:19

标签: python django unicode

编辑:我找到了解决办法。通过添加smart_unicode(来自django.utils.encoding import smart_unicode)并在输出错误的字符串前放置'u',并在设置部分添加DEFAULT_CHARSET ='utf-8' - 测试运行。

他们失败了。但他们跑了。问题已结束。 ~~~~~~~~~~~~~~~~~~~~~~~~~

使用Django 1.6.7

你好,

尝试从这个github运行这个Django代码的测试(一个允许在Django Oscar中进行条带支付的项目):

#!/usr/bin/env python
import sys
from coverage import coverage
from optparse import OptionParser

from django.conf import settings


if not settings.configured:
    settings.configure(
            DATABASES={
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    }
                },
            INSTALLED_APPS=[
                'django.contrib.auth',
                'django.contrib.admin',
                'django.contrib.contenttypes',
                'django.contrib.sessions',
                'django.contrib.sites',
                'oscar_stripe',
                'south',
                ],
            DEBUG=False,
            SITE_ID=1,
            NOSE_ARGS=['-s', '--with-spec'],
        )

# Needs to be here to avoid missing SETTINGS env var 
from django_nose import NoseTestSuiteRunner


def run_tests(*test_args):
    if 'south' in settings.INSTALLED_APPS:
        from south.management.commands import patch_for_test_db_setup
        patch_for_test_db_setup()

    if not test_args:
        test_args = ['tests']

    # Run tests
    test_runner = NoseTestSuiteRunner(verbosity=1)

    c = coverage(source=['oscar_stripe'], omit=['*migrations*', '*tests*'])
    c.start()
    num_failures = test_runner.run_tests('test_args')
    c.stop()

    if num_failures > 0:
        sys.exit(num_failures)
    print "Generating HTML coverage report"
    c.html_report()


if __name__ == '__main__':
    parser = OptionParser()
    (options, args) = parser.parse_args()
    run_tests('*args')

我收到错误:

/stripetest/django-oscar-stripe$ ./runtests.py
nosetests --verbosity 1 t e s t _ a r g s -s --with-spec
Creating test database for alias 'default'...

Unloadable or unexecutable test.

A Failure case is placed in a test suite to indicate the presence of a
test that could not be loaded or executed. A common example is a test
module that fails to import.
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
- runTest (ERROR)
Traceback (most recent call last):
  File "./runtests.py", line 60, in <module>
    run_tests('*args')
  File "./runtests.py", line 48, in run_tests
    num_failures = test_runner.run_tests('test_args')
  File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/django_nose/runner.py", line 155, in run_tests
result = self.run_suite(nose_argv)
File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/django_nose/runner.py", line 117, in run_suite
    addplugins=plugins_to_add)
  File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/nose/core.py", line 121, in __init__
    **extra_args)
  File "/usr/lib/python2.7/unittest/main.py", line 95, in __init__
    self.runTests()
  File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/nose/core.py",     line 207, in runTests
    result = self.testRunner.run(self.test)
  File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/nose/core.py", line 66, in run
    result.printErrors()
  File "/home/david/.virtualenvs/stripetest/local/lib/python2.7/site-packages/nose/result.py", line 103, in printErrors
    _TextTestResult.printErrors(self)
  File "/usr/lib/python2.7/unittest/runner.py", line 107, in printErrors
    self.stream.writeln()
  File "/usr/lib/python2.7/unittest/runner.py", line 25, in writeln
    self.write('\n') # text-mode streams translate to \r\n if needed
TypeError: unicode argument expected, got 'str'

我一直在争夺它几个小时,而且我无处可去。我已经尝试过滤相关的行到unicode(从('test_args')到(u'test_args'))。同样的错误。通过foo.encode过滤('utf-8')。同样的错误。

非常感谢任何帮助。因为它不是我的代码,而且我从未使用过Nose,覆盖或者optparse,我正在挣扎。

感谢。

0 个答案:

没有答案