我有一个简单的抽象类,我想为此编写一个单元测试。 我正在使用Django 1.10而且answers I have found多数都在那里多年,可能已经过时了。 我尝试过Vinod Kurup的解决方案:
# tests/test_foo.py
from django.db import models
from django.test import TestCase
from ..models import MyAbstractModel
class MyTestModel(MyAbstractModel):
name = models.CharField(max_length=20)
class Meta:
app_label = 'myappname'
class AbstractTest(TestCase):
def test_my_test_model(self):
self.assertTrue(MyTestModel.objects.create(name='foo'))
是否有任何简单的现代方法来测试与Django 1.10一样的抽象模型,就像来自Vinod Kurup的抽象模型一样?
修改
我的抽象模型代码:
class FlagsModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
reported = models.BooleanField(default=False)
deleted = models.BooleanField(default=False)
class Meta:
abstract = True
这是我的测试文件:
from ..models import RecipeModel, FlagsModel
class FlagsTestModel(FlagsModel):
class Meta:
app_label = 'recipes'
class FlagsModelAbstractTest(TestCase):
def test_my_test_model(self):
self.assertTrue(FlagsTestModel.objects.create())
我得到的错误是:
Traceback (most recent call last):
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "recipes_flagstestmodel" does not exist
LINE 1: ...eported", "recipes_flagstestmodel"."deleted" FROM "recipes_f...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/management/commands/test.py", line 72, in handle
failures = test_runner.run_tests(test_labels)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/test/runner.py", line 549, in run_tests
old_config = self.setup_databases()
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/test/runner.py", line 499, in setup_databases
self.parallel, **kwargs
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/test/runner.py", line 743, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 78, in create_test_db
self.connection._test_serialized_contents = self.serialize_db_to_string()
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 122, in serialize_db_to_string
serializers.serialize("json", get_objects(), indent=None, stream=out)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 129, in serialize
s.serialize(queryset, **options)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/core/serializers/base.py", line 79, in serialize
for count, obj in enumerate(queryset, start=1):
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 118, in get_objects
for obj in queryset.iterator():
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/arch/.environments/djanveg_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "recipes_flagstestmodel" does not exist
LINE 1: ...eported", "recipes_flagstestmodel"."deleted" FROM "recipes_f...
答案 0 :(得分:1)
问题是你没有将那个模型迁移到你的数据库,那是因为你在tests文件中创建了模型,Django只检查models.py
文件中的模型,所以将下一个代码移到models.py
文件:
class FlagsTestModel(FlagsModel):
class Meta:
app_label = 'recipes'
然后运行python manage.py makemigrations
和python manage.py mgirate
在数据库中创建表,一切都将按预期工作。