简体   繁体   English

加载灯具 Django

[英]Loading Fixtures Django

I'm trying to load some fixtures within my Django tests, but they don't seem to load.我正在尝试在我的 Django 测试中加载一些装置,但它们似乎没有加载。

In my settings.py, I specify:在我的 settings.py 中,我指定:

FIXTURE_DIRS = (os.path.join(PROJECT_DIR, 'dhtmlScheduler\\fixtures\\'))

Now, within my test case:现在,在我的测试用例中:

def setUp(self):
    fixtures = ['users.json', 'employee.json']

I should also probably mention that I'm using the Nose test runner:我还应该提到我正在使用 Nose 测试运行器:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

and unittest:和单元测试:

class TestEmployee(unittest.TestCase):

I must be missing something obvious, can someone point me in the right direction?我一定遗漏了一些明显的东西,有人能指出我正确的方向吗?

FIXTURE_DIRS is supposed to be a list or tuple, not a string. FIXTURE_DIRS 应该是一个列表或元组,而不是一个字符串。 Remember that it's the comma that defines a tuple litteral, not the parens, IOW your settings should be请记住,定义元组文字的是逗号,而不是括号,您的设置应该是

FIXTURE_DIRS = (
    os.path.join(PROJECT_DIR, 'dhtmlScheduler\\fixtures\\'), 
    )

As a side note, hardcoding the path separator kind of defeats the whole point of using os.path.join(), so this should really be:作为旁注,硬编码路径分隔符会破坏使用 os.path.join() 的全部意义,所以这应该是:

FIXTURE_DIRS = (
    os.path.join(PROJECT_DIR, 'dhtmlScheduler', 'fixtures'), 
    )

Edit : and finally, you have to declare your TestCase fixtures at the class level, not in the setUp() method...编辑:最后,您必须在类级别而不是在 setUp() 方法中声明您的 TestCase 装置...

FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)

Or要么

from django.test import TestCase

class MyTestCase(TestCase):
     fixtures = [
        '/myapp/fixtures/users.json', 
        '/myapp/fixtures/employee.json'
     ]

In your test case file, Simply make reference to your predefined fixtures in this manner as shown below在您的测试用例文件中,只需以这种方式引用您的预定义装置,如下所示在此处输入图片说明

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

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