请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

运行测试报错

根据第二章第7节,对项目运行test,报错如下:

在这里输入代码Testing started at 下午4:59 ...
/root/.local/share/virtualenvs/zanhu-MHdnJQJv/bin/python /data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_manage.py test /data/project/py/bb/zanhu
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
    result.read()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1366, "Incorrect string value: '\\xE7\\x94\\xA8\\xE6\\x88\\xB7' for column `test_zanhu`.`auth_permission`.`name` at row 1")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_manage.py", line 168, in <module>
    utility.execute()
  File "/data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_manage.py", line 142, in execute
    _create_command().run_from_argv(self.argv)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_manage.py", line 104, in handle
    failures = TestRunner(test_labels, **options)
  File "/data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_runner.py", line 255, in run_tests
    extra_tests=extra_tests, **options)
  File "/data/soft/pycharm-2019.2/plugins/python/helpers/pycharm/django_test_runner.py", line 156, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/test/runner.py", line 604, in run_tests
    old_config = self.setup_databases()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/test/runner.py", line 551, in setup_databases
    self.parallel, **kwargs
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/test/utils.py", line 174, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 68, in create_test_db
    run_syncdb=True,
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 226, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/core/management/sql.py", line 51, in emit_post_migrate_signal
    **kwargs
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/contrib/auth/management/__init__.py", line 79, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/models/query.py", line 465, in bulk_create
    ids = self._batched_insert(objs_without_pk, fields, batch_size)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/models/query.py", line 1155, in _batched_insert
    self._insert(item, fields=fields, using=self.db)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/models/query.py", line 1136, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sql
    cursor.execute(sql, params)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
    result.read()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/root/.local/share/virtualenvs/zanhu-MHdnJQJv/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE7\\x94\\xA8\\xE6\\x88\\xB7' for column `test_zanhu`.`auth_permission`.`name` at row 1")

Process finished with exit code 1

正在回答 回答被采纳积分+3

2回答

慕容2094767 2020-09-17 18:54:14

Centos 系统  试过其它方法,不管用,最终通过改配置文件解决了

找到my.cnf文件

在my.cnf文件里面配置如下:

character-set-server=utf8

collation-server=utf8_general_ci

重启mysql

service mysqld restart

删除数据库重新执行


0 回复 有任何疑惑可以回复我~
  • Jack #1
    谢谢!删除的是测试用例的数据库吗
    回复 有任何疑惑可以回复我~ 2020-09-17 23:08:59
Jack 2020-04-02 09:26:45

django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE7\\x94\\xA8\\xE6\\x88\\xB7' for column `test_zanhu`.`auth_permission`.`name` at row 1")

这是给某个字段赋值错误了,类型与数据库字段里定义的不匹配。你检查下测试用例里写的值,还有问题的话截图一下代码吧~

0 回复 有任何疑惑可以回复我~
  • 提问者 慕仔3205528 #1
    我检查了一下,是运行测试时生成的临时数据库的默认编码不是utf8,按照这个文章在base.py添加# DATABASES = {
    #     'default':
    #     {
    #         'ENGINE': 'django.db.backends.mysql',
    #         'TEST': {
    #             'CHARSET' : 'utf8',
    #             'COLLATION':'utf8_general_ci'
    #         }
    #     },
    # }设置,没有起作用,最终改了数据库的配置文件,增加[mysqld]
    character_set_server=utf8并重启,测试就能走下去了
    回复 有任何疑惑可以回复我~ 2020-04-02 09:47:44
  • Jack 回复 提问者 慕仔3205528 #2
    棒极了!
    回复 有任何疑惑可以回复我~ 2020-04-02 12:18:19
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信