在 Django 上测试应用程序时未找到固定装置
我最终决定为我的所有代码创建单元测试。也就是说,我正在尝试根据我的模型创建夹具来测试我的功能。
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有几件事可能会有所帮助:
There are a couple things that could help:
您是否检查过夹具所在的路径是否可用:
Have you checked if the path where the fixture is present is available in:
我的名字有问题。即使我将文件放在应用程序文件夹下的固定目录中,下面的代码也无法工作。
然后我将导入语句更改为它刚刚起作用的下面。
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.
I then changed the import statement to below an it just worked.
我遇到了同样的问题,它对我不起作用的原因是我的初始数据文件没有扩展名。
我做了:
这不起作用,因为initial_data没有扩展,但是,这确实有效:
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:
Which doesnt work, since there's no extension on initial_data, however, this does work:
只是为了确认 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
... DONT USE IT ... as suggested use