简体   繁体   English

在Django上测试应用程序时没有找到任何装置

[英]No fixtures found when testing an app on Django

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. 我使用dumpdata命令创建了一个json fixture,并将它放在我的app上的fixtures目录下。 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: 我在Arch Linux机器上使用以下命令运行我的测试:

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. 它创建一个sqlite数据库并安装所有表和indeces,但最后它说没有找到夹具,它说我的测试失败,因为它没有找到任何数据。

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: 我正在运行Django开发版的最新版本,并注意到根据文档,我应该使用以下方法导入unittest:

from django.utils import unittest

However, when I do this, it complains that unittest cannot be imported. 但是,当我这样做时,它抱怨无法导入unittest。 That's why I import unittest directly from my python path which worked. 这就是我直接从我的python路径导入unittest的原因。

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. 我以前试过模拟django模型对象,但我认为使用fixture来测试Django应用比使用模拟库更好。 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. 编辑:如果我将我的灯具名称更改为initial_data.json,它将在每次运行我的测试时加载它。 However, I still need to have multiple fixture names for running different tests. 但是,我仍然需要有多个灯具名称来运行不同的测试。

EDIT: I got it working by importing TestCase from the following: 编辑:我通过从以下导入TestCase得到它:

from django.test import TestCase

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. 您可以使用--verbosity = 2运行测试,以查看django查找夹具的目录。
  2. django TestCase is what django recommends to use, opposed to unittest. django TestCase是django建议使用的,与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. 如果它不在你的应用程序中,你必须在设置文件中指定一个fixture目录。

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: 哪个不起作用,因为initial_data没有扩展,但是,这确实有效:

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

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

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

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. 即使我在app文件夹下的fixture目录中有文件,下面的代码也行不通。

fixtures = ['initial_data2.json'] fixtures = ['initial_data2.json']

I then changed the import statement to below an it just worked. 然后我将import语句更改为刚刚工作的下面的语句。

from django.test import TestCase 来自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 只是为了确认TestCase的import语句会破坏查找任何灯具但是以其他方式工作,所以只是没有错误,因为它尝试没有夹具加载是

from django.utils.unittest import TestCase

... DONT USE IT ... as suggested use ...不要使用它...作为建议使用

from django.test import TestCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM