在 Django 上测试应用程序时未找到固定装置

发布于 2024-11-07 12:36:30 字数 963 浏览 0 评论 0原文

我最终决定为我的所有代码创建单元测试。也就是说,我正在尝试根据我的模型创建夹具来测试我的功能。

我使用 dumpdata 命令创建了一个 json 固定装置,并将其放置在我的应用程序的固定装置目录下。以下是我的测试代码:

import unittest
from mysite.myapp.models import Post

class RatingTestCase(unittest.TestCase):
  fixtures = [
      'posts.json',
  ]

def test_me(self):
  p = Post.objects.all()
  self.assertTrue(p)

我在 Arch Linux 机器上使用以下命令运行测试:

python2.7 manage.py test myapp

它创建一个 sqlite 数据库并安装所有表和索引,但最后它说没有找到固定装置,它说我的测试失败,因为没有找到任何数据。

我正在运行 Django 开发版本的最新版本,并注意到根据文档,我应该使用以下命令导入unittest:

from django.utils import unittest

但是,当我这样做时,它抱怨说无法导入单元测试。这就是为什么我导入 直接从我的 python 路径进行单元测试,效果很好。

我之前尝试过模拟 django 模型对象,但我认为使用固定装置测试 Django 应用程序比使用模拟库更好。知道如何加载灯具吗?

提前致谢。

编辑:如果我将夹具名称更改为initial_data.json,它会在每次运行测试时加载它。但是,我仍然需要有多个夹具名称来运行不同的测试。

编辑:我通过从以下位置导入 TestCase 使其工作:

from django.test import TestCase

I have finally decided to create unit test for all my code. That is I'm trying to create fixtures based on my models to test my function against them.

I have create a json fixture using dumpdata command and placed it under fixtures directory on my app. The following is the code on my test:

import unittest
from mysite.myapp.models import Post

class RatingTestCase(unittest.TestCase):
  fixtures = [
      'posts.json',
  ]

def test_me(self):
  p = Post.objects.all()
  self.assertTrue(p)

I run my test using the following command on my Arch Linux machine:

python2.7 manage.py test myapp

It creates a sqlite database and installs all the tables and indeces, however at the end it says no fixtures found and it says my test failed since it didn't find any data.

I'm running the latest revision of development version of Django and noticed that based on the documentation I am supposed to import unittest using:

from django.utils import unittest

However, when I do this, it complains that unittest cannot be imported. That's why I import
unittest directly from my python path which worked.

I've tried to mock django model objects before, but I think it's better to test Django apps using fixtures than using mock libraries. Any idea how to load the fixtures?

Thanks in advance.

EDIT: If I change my fixture name to initial_data.json, it will load it every time I run my test. However, I still need to have multiple fixture names for running different tests.

EDIT: I got it working by importing TestCase from the following:

from django.test import TestCase

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雪化雨蝶 2024-11-14 12:36:30

有几件事可能会有所帮助:

  1. 您可以使用 --verbosity=2 运行测试来查看 django 在哪些目录中查找固定装置。
  2. django TestCase是django推荐使用的,而不是unittest。
  3. 你的装置目录/文件位于哪里?如果它不在您的应用程序中,您必须在设置文件中指定一个固定装置目录。

There are a couple things that could help:

  1. You can run the test with --verbosity=2 to see which directories django looks in for fixtures.
  2. django TestCase is what django recommends to use, opposed to unittest.
  3. Where is your fixture directory/file located? If it's not in your app you have to specify a fixture directory in your settings file.
风透绣罗衣 2024-11-14 12:36:30

您是否检查过夹具所在的路径是否可用:

settings.FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)

Have you checked if the path where the fixture is present is available in:

settings.FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
蓝色星空 2024-11-14 12:36:30

我的名字有问题。即使我将文件放在应用程序文件夹下的固定目录中,下面的代码也无法工作。

赛程 = ['initial_data2.json']

然后我将导入语句更改为它刚刚起作用的下面。

从 django.test 导入测试用例

I had the name problem. The code below would not work even though I had the file in my fixture directory under the app folder.

fixtures = ['initial_data2.json']

I then changed the import statement to below an it just worked.

from django.test import TestCase

一个人的夜不怕黑 2024-11-14 12:36:30

我遇到了同样的问题,它对我不起作用的原因是我的初始数据文件没有扩展名。

我做了:

manage.py dumpdata > initial_data
manage.py loaddata initial_data

这不起作用,因为initial_data没有扩展,但是,这确实有效:

mv initial_data initial_data.json 
manage.py loaddata --verbosity=2 initial_data.json 

I had the same problem, and the reason it didnt work for me was that I had no extension on my initial-data-file.

I did:

manage.py dumpdata > initial_data
manage.py loaddata initial_data

Which doesnt work, since there's no extension on initial_data, however, this does work:

mv initial_data initial_data.json 
manage.py loaddata --verbosity=2 initial_data.json 
两仪 2024-11-14 12:36:30

只是为了确认 TestCase 的导入语句无法找到任何固定装置
但以所有其他方式工作,所以不会抛出任何错误,因为它尝试不加载任何夹具

from django.utils.unittest import TestCase

......不要使用它......按照建议使用

from django.test import TestCase

Just to confirm the import statement for TestCase that breaks finding any fixtures
but works in all other ways, so just throws no errors since it attempts no fixture loading is

from django.utils.unittest import TestCase

... DONT USE IT ... as suggested use

from django.test import TestCase
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文