运行测试报错

来源:2-7 一定要编写测试用例

慕仔3205528

2020-03-31

根据第二章第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

写回答

2回答

慕容2094767

2020-09-17

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

找到my.cnf文件

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

character-set-server=utf8

collation-server=utf8_general_ci

重启mysql

service mysqld restart

删除数据库重新执行


0
1
Jack
谢谢!删除的是测试用例的数据库吗
2020-09-17
共1条回复

Jack

2020-04-02

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
2
Jack
回复
慕仔3205528
棒极了!
2020-04-02
共2条回复

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程