gpt4 book ai didi

python - 如何在运行 Django 测试之前加载测试 yaml 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:06 25 4
gpt4 key购买 nike

我正在使用 Django 和 Python 3.7。我想在运行测试之前加载一些测试数据。我认为在我的测试中指定“fixtures”元素可以做到这一点,但它似乎没有被加载。我使用以下内容创建了文件 mainpage/fixtures/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

我在下面的测试文件中指定了 fixture ...

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

我尝试将文件名更改为根本不存在的名称,以查看是否收到不同的错误,但没有。所以我认为这个“Fixtures”惯例根本不起作用。如何在运行测试之前加载测试数据?

最佳答案

为了以这种方式使用固定装置,需要设置TransactionTestCase.fixtures 1

加载装置的神奇发生在TransactionTestCase中。这使得测试类的子类 TransactionTestCase 例如django.test.TestCase 还会加载在fixtures 属性中指定的fixtures。 2

当前的TestModels测试类是unitest.TestCase的子类,因此对装置设置不执行任何操作。 3

from django.test import TestCase

class TestModels(TestCase):
fixtures = ['test_data.yaml',]

通常,设置 fixture 文件的名称而不是 fixture 文件的整个路径就可以了。如果您需要为要在其中发现的装置设置自定义文件夹,可以通过设置 FIXTURE_DIRS 4 来指定。

关于python - 如何在运行 Django 测试之前加载测试 yaml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54446003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com