测试出错

来源:6-12 模型类的测试用例test_models.py

JackCai20

2021-03-26

Testing started at 12:52 …
ssh://root@192.168.110.130:22/root/.virtualenvs/zanhu01/bin/python3.7 -u /root/.pycharm_helpers/pycharm/django_test_manage.py test zanhu.news.tests.test_models.NewsModelsTest /root/my_project/zanhu
Creating test database for alias ‘default’…
Got an error creating the test database: (1007, “Can’t create database ‘test_zanhu’; database exists”)
Type ‘yes’ if you would like to try deleting the test database ‘test_zanhu’, or ‘no’ to cancel: yes
yes
Destroying old test database for alias ‘default’…
Traceback (most recent call last):
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/mysql/base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 206, in execute
res = self._query(query)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 312, in _query
db.query(q)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/connections.py”, line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1366, “Incorrect string value: ‘\xE7\x94\xA8\xE6\x88\xB7’ for column ‘name’ at row 1”)

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

Traceback (most recent call last):
File “/root/.pycharm_helpers/pycharm/django_test_manage.py”, line 168, in
utility.execute()
File “/root/.pycharm_helpers/pycharm/django_test_manage.py”, line 142, in execute
_create_command().run_from_argv(self.argv)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/commands/test.py”, line 26, in run_from_argv
super().run_from_argv(argv)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/base.py”, line 316, in run_from_argv
self.execute(*args, **cmd_options)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/base.py”, line 353, in execute
output = self.handle(*args, **options)
File “/root/.pycharm_helpers/pycharm/django_test_manage.py”, line 104, in handle
failures = TestRunner(test_labels, **options)
File “/root/.pycharm_helpers/pycharm/django_test_runner.py”, line 255, in run_tests
extra_tests=extra_tests, **options)
File “/root/.pycharm_helpers/pycharm/django_test_runner.py”, line 156, in run_tests
return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/test/runner.py”, line 604, in run_tests
old_config = self.setup_databases()
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/test/runner.py”, line 551, in setup_databases
self.parallel, **kwargs
File “/root/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/base/creation.py”, line 68, in create_test_db
run_syncdb=True,
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/init.py”, line 148, in call_command
return command.execute(*args, **defaults)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/base.py”, line 353, in execute
output = self.handle(*args, **options)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/base.py”, line 83, in wrapped
res = handle_func(*args, **kwargs)
File “/root/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/core/management/sql.py”, line 51, in emit_post_migrate_signal
**kwargs
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/dispatch/dispatcher.py”, line 175, in send
for receiver in self._live_receivers(sender)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/dispatch/dispatcher.py”, line 175, in
for receiver in self._live_receivers(sender)
File “/root/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/models/sql/compiler.py”, line 1289, in execute_sql
cursor.execute(sql, params)
File “/root/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/utils.py”, line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “/root/.virtualenvs/zanhu01/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/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/django/db/backends/mysql/base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 206, in execute
res = self._query(query)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 312, in _query
db.query(q)
File “/root/.virtualenvs/zanhu01/lib/python3.7/site-packages/MySQLdb/connections.py”, line 224, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1366, “Incorrect string value: ‘\xE7\x94\xA8\xE6\x88\xB7’ for column ‘name’ at row 1”)

Process finished with exit code 1
Empty test suite.

写回答

2回答

JackCai20

提问者

2021-03-27

已经完美解决。

本次错误是每当我使用pycharm写测试程序运行的时候, 会自动创建一个test_zanhu数据库, 但是每次创建的时候都会报错

问题在于编码格式问题


解决办法:

1. 可以先进入数据库,

mysql -u root -p

输入命令

show variables like '%char%';

(查看编码格式)


2. 退出数据库,之后输入:

vim /etc/my.cnf

在配置文件里输入

[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8


3.再次进入数据库,使用同样命令:

show variables like '%char%';

可以发现编码格式改变。


4.完美运行测试代码

0
1
Jack
好的。
2021-03-27
共1条回复

Jack

2021-03-26

您好,粘贴代码可以编辑器格式

MySQLdb._exceptions.OperationalError: (1366, “Incorrect string value: ‘\xE7\x94\xA8\xE6\x88\xB7’ for column ‘name’ at row 1”)

这里是关键,像是name这个字段的属性问题,填入的值不符合要求

0
1
JackCai20
那应该怎么该呢。。。。。。 我建的数据表应该没问题呀,测试代码也是和你一样的。
2021-03-26
共1条回复

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

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

900 学习 · 756 问题

查看课程