Я пытаюсь написать контрольные примеры, используя класс TestCase в python.Когда я запускаю python manage.test, я получаю следующую ошибку:
Operations to perform:
Synchronize unmigrated apps: messages, rest_framework, staticfiles
Apply all migrations: admin, auth, contenttypes, genes, sessions
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application rest_framework
Running pre-migrate handlers for application files
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
Applying contenttypes.0001_initial... OK (0.031s)
Applying auth.0001_initial... OK (0.272s)
Applying admin.0001_initial... OK (0.082s)
Applying admin.0002_logentry_remove_auto_add... OK (0.011s)
Applying contenttypes.0002_remove_content_type_name... OK (0.064s)
Applying auth.0002_alter_permission_name_max_length... OK (0.029s)
Applying auth.0003_alter_user_email_max_length... OK (0.028s)
Applying auth.0004_alter_user_username_opts... OK (0.012s)
Applying auth.0005_alter_user_last_login_null... OK (0.035s)
Applying auth.0006_require_contenttypes_0002... OK (0.006s)
Applying auth.0007_alter_validators_add_error_messages... OK (0.011s)
Applying auth.0008_alter_user_username_max_length... OK (0.046s)
Applying auth.0009_alter_user_last_name_max_length... OK (0.040s)
Applying files.0001_initial... OK (0.024s)
Applying files.0002_auto_20190303_1656... OK (0.005s)
Applying sessions.0001_initial... OK (0.028s)
Running post-migrate handlers for application admin
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application auth
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application contenttypes
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application sessions
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application rest_framework
Running post-migrate handlers for application files
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Traceback (most recent call last):
File "/Python/myenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "//Python/myenv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 312, in _query
db.query(q)
File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/connections.py", line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1146, "Table 'test_mydb.mytable’ doesn't exist")
Вот мой tests.py: класс TestCase (TestCase):
@classmethod
def setUpTestData(cls):
print("calling command")
db_dump = “’file_data.json'"
call_command("loaddata",db_dump,verbosity=3)
def test_statuscode(self ):
print("Starting test cases....")
url=‘/find_file/‘
query_params ={'spec’:’detailed’,’name’:’ABV’}
response = self.client.get(url,query_params)
self.assertEqual(response.status_code,200)
А вот файлы / модели.py
class MyModel(models.Model):
spec = models.CharField(max_length=255, blank=True, null=True)
id = models.CharField(primary_key=True, max_length=128)
name = models.CharField(max_length=128, blank=True, null=True)
number = models.CharField(max_length=60, blank=True, null=True)
db = models.CharField(max_length=32)
class Meta:
managed = True
db_table = ‘mytable’
settings.py
from .base import *
DATABASES = {
'default' :{
'ENGINE' : 'django.db.backends.mysql',
'NAME': ‘mydb_90',
'USER': ‘xxx’,
'PASSWORD': ‘yyy’,
'PORT' : '3306',
},
}
Версия Django - 2.0.6.Python 3.7.1.У меня есть 2 вопроса: 1) Почему я получаю эту ошибку?Файлы миграции на месте, и журнал показывает, что эти файлы выполняются.2) Я ожидал, что создание БД и таблицы произойдет в методе setupTestData.Похоже, базы данных и таблицы создаются до вызова этого метода.Так что я не знаю, как его отладить.