【问题标题】:Django shell: Command to load test fixture data?Django shell:加载测试夹具数据的命令?
【发布时间】:2012-12-17 15:15:49
【问题描述】:

有没有一种简单的方法来加载我通常在交互式 Django shell 中的自动化测试运行中使用的夹具数据?

混合来自数据库的模型数据和来自夹具的其他数据可能会很尴尬。就我而言,我有一些只读表,并想尝试一些我以后可以丢弃的数据。

我可能可以像here 描述的那样加载夹具文件,但是重复使用有点麻烦......

【问题讨论】:

    标签: django shell


    【解决方案1】:

    我希望./manage.py loaddata fixture_name.json 是你想要的。

    【讨论】:

    • 谢谢,我想要在 shell 中可用的数据,即
    【解决方案2】:

    ilardm 的回答指向了正确的方向,特别是你想要的是:

    from django.core.management import call_command
    call_command('loaddata', 'fixture_name.json')
    

    编辑:但是在测试用例中包含固定装置的正确方法是这样的:

    class TestThis(TestCase):
        fixtures = ['myfixture.json']
    
        def setUp(self):
            # Ready to test
    

    【讨论】:

    • 在以前的 django 版本中你可以使用:from django.core import management from django.core.management.commands import loaddata management.call_command('loaddata', 'test_data')
    【解决方案3】:

    也许这个链接:http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 2011-01-12
      • 2018-02-07
      • 2011-03-10
      • 2013-10-24
      • 2015-01-01
      • 2011-04-16
      • 2013-05-11
      • 2018-02-03
      相关资源
      最近更新 更多