运行unittest时的ValidationError

时间:2015-04-27 16:39:39

标签: django unit-testing

我正在尝试在django应用上开始测试。该应用程序工作正常,但我想在进行一些重构之前设置测试。现在我只是尝试做一个简单的虚拟测试:

import unittest

class TestBasic(unittest.TestCase):
    "Basic tests"

def test_basic(self):
    a = 1
    self.assertEqual(1, a)

def test_basic_2(self):
    a = 1
    assert a == 1

我得到的错误似乎与测试本身没有任何关系:

Traceback (most recent call last):
  File "Y:\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
385, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\commands\test.py",
line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\commands\test.py",
line 71, in execute
    super(Command, self).execute(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\test.py",
line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Python27\lib\site-packages\django\test\runner.py", line 147, in run_t
ests
    old_config = self.setup_databases()
  File "C:\Python27\lib\site-packages\django\test\runner.py", line 109, in setup
_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "C:\Python27\lib\site-packages\django\test\runner.py", line 299, in setup
_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "C:\Python27\lib\site-packages\django\db\backends\creation.py", line 377,
 in create_test_db
    test_flush=True,
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
115, in call_command
    return klass.execute(*args, **defaults)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py
", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 68
, in migrate
    self.apply_migration(migration, fake=fake)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 10
2, in apply_migration
    migration.apply(project_state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 1
08, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, ne
w_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\models.py"
, line 36, in database_forwards
    schema_editor.create_model(model)
  File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 212, i
n create_model
    definition, extra_params = self.column_sql(model, field)
  File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 124, i
n column_sql
    default_value = self.effective_default(field)
  File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 187, i
n effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 627, in get_db_prep_save
    prepared=False)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 1176, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 1171, in get_prep_value
    return self.to_python(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
 1141, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid date format.
It must be in YYYY-MM-DD format."]

创建测试数据库时似乎是一个错误。怎么会抛出验证错误?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果你正在运行Django测试,特别是那些涉及数据库的测试,你应该对Django TestCase进行子类化,而不是unittest.TestCase

from django.test import TestCase

我很确定这会解决您的问题,但可能是您的环境存在其他问题。

编辑: 请参阅此处的文档:https://docs.djangoproject.com/en/1.8/topics/testing/overview/

编辑2: 您的某个型号的models.DateTimeField声明中是否有错误?这可能会导致验证错误。

答案 1 :(得分:0)

在同一个项目的另一个应用中,过去的某个迁移(可能南方未检测到)之一出错。我通过运行找到了这个:manage.py test myapp -v 2 所以我确保数据库中的所有内容都是最新的,我删除了迁移。不行。