我如何运行我的Django的测试之前加载我的测试YAML文件?

问题描述 投票:1回答:2

我使用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

我试着改变文件名的东西,它根本不存在,看看如果我得到一个不同的错误,但我不知道。所以,我不认为这“灯具”约定的所有工作。我如何得到我的加载测试数据运行我的测试之前?

python django python-3.x unit-testing fixtures
2个回答
0
投票

为了使用夹具这种方式,TransactionTestCase.fixtures需要设置。 1

装入灯具魔术发生在qazxsw POI。这使得它,这样子类qazxsw POIË该测试类。 qazxsw也POI装载在夹具指定夹具属性。 TransactionTestCase

目前TransactionTestCase测试类的子类django.test.TestCase,因此不做任何处理的灯具安装。 2

TestModels

通常它是蛮好的夹具文件,而不是整个路径夹具文件中设置的名称。如果您需要设置一个自定义文件夹中被发现灯具,可通过设置unitest.TestCase 3指定


0
投票

你应该(如@OluwafemiSule)中,以类使用from django.test import TestCase class TestModels(TestCase): fixtures = ['test_data.yaml',] 他的回答指出属性FIXTURE_DIRS考虑。

你可以实现你的测试和负载灯具使用4

虽然不推荐,你可以在你这样的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命令,你会发现这是一个非常有用的工具。

© www.soinside.com 2019 - 2024. All rights reserved.