我使用Django和Python 3.7。我想我的运行测试之前加载一些测试数据。我想指定在我的测试会做这样的“夹具”的元素,但它似乎并没有被加载。我创建的文件,炫魅/夹具/ test_data.yaml,与此内容
model: mainpage.website
pk: 1
fields:
path: /testsite
model: mainpage.article
pk: 1
fields:
website: 1
title: 'mytitle'
path: '/test-path'
url: 'http://www.mdxomein.com/path'
created_on:
type: datetime
columnDefinition: TIMESTAMP DEFAULT CURRENT_TIMESTAMP
model: mainpage.articlestat:
pk: 1
fields:
article: 1
elapsed_time_in_seconds: 300
hits: 2
我在下面列出我的测试文件夹具...
from django.test import TestCase
from mainpage.models import ArticleStat, Article
import unittest
class TestModels(unittest.TestCase):
fixtures = ['/mainpage/fixtures/test_data.yaml',]
# Test saving an article stat that hasn't previously
# existed
def test_add_articlestat(self):
id = 1
article = Article.objects.filter(id=id)
self.assertTrue(article, "A pre-condition of this test is that an article exist with id=" + str(id))
articlestat = ArticleStat(article=article,elapsed_time_in_seconds=250,votes=25,comments=15)
articlestat.save()
article_stat = ArticleStat.objects.get(article=article)
self.assertTrue(article_stat, "Failed to svae article stat properly.")
但它不会出现运行我的测试时,任何测试数据被加载中...
(venv) localhost:mainpage_project davea$ cd /Users/davea/Documents/workspace/mainpage_project; source ./venv/bin/activate; python manage.py test
test activated!
Creating test database for alias 'default'...
/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField Article.front_page_first_appeared_date received a naive datetime (2019-01-30 17:02:31.329751) while time zone support is active.
RuntimeWarning)
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_add_articlestat (mainpage.tests.TestModels)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/mainpage_project/mainpage/tests.py", line 15, in test_add_articlestat
self.assertTrue(article, "A pre-condition of this test is that an article exist with id=" + str(id))
AssertionError: <QuerySet []> is not true : A pre-condition of this test is that an article exist with id=1
----------------------------------------------------------------------
Ran 1 test in 0.001s
我试着改变文件名的东西,它根本不存在,看看如果我得到一个不同的错误,但我不知道。所以,我不认为这“灯具”约定的所有工作。我如何得到我的加载测试数据运行我的测试之前?
你应该(如@OluwafemiSule)中,以类使用from django.test import TestCase
class TestModels(TestCase):
fixtures = ['test_data.yaml',]
他的回答指出属性FIXTURE_DIRS
考虑。
虽然不推荐,你可以在你这样的django.test.TestCase
方法编程方式加载您的灯具:
fixtures
但这种方法需要你回滚您对加载的数据进行任何改变,如果你在随后的测试中使用它。
从节中Django的测试文档unittest.TestCase
你可以阅读:
该Django的测试用例的问候灯具为您做最重要的事情是,它保持一致的状态为您的所有测试。在运行每个测试之前,数据库被刷新:其返回到原始状态(比如你的第一个执行syncdb后)。那么你的灯具被加载到数据库,然后设置()被调用,那么您的测试运行,然后拆除()被调用。保持相互绝缘的测试,当你试图做一个很好的测试套件是非常重要的。
这就是为什么你应该使用setUp
。
另一个重要的技巧是,你可以使用命令from django.core.management import call_command
def setUp(self):
call_command('loaddata', 'initial_data.yml', app_label='myapp')
,看看你的灯具都不错:
Fixtures in Unit Tests
这将使用从给定的固定装置(一个或多个)数据运行的Django开发服务器(如在的runserver)。你可以阅读有关django.test.TestCase
命令,你会发现这是一个非常有用的工具。