A trick in loading Fixture to test django application

本文详细介绍了如何在Django的测试用例中使用fixtures来填充测试数据,包括在setUpClass方法中加载fixtures的方法,以及在PyDev环境下运行测试时遇到的问题及解决方案。

A test case for a database-backed Web site isn't much use if there isn't any data in the database. To make it easy to put test data into the database, Django's custom TestCase class provides a way of loading fixtures.(see detail here)

Once you've created a fixture and placed it in a fixtures directory in one of your INSTALLED_APPS, you can use it in your unit tests by specifying a fixtures class attribute on your django.test.TestCase subclass:

from django.test import TestCase
from myapp.models import Animal

class AnimalTestCase(TestCase):
    fixtures = ['myfixture.json']

    def setUp(self):
        # Test definitions as before.
        call_setup_methods()

    def testFluffyAnimals(self):
        # A test that uses the fixtures.
        call_some_test_code()

However, if you need to add setUpClass(), the fixtures won't work.

In fact, fixture can't be inserted before setUpcClass() excuted.

To solve this problem, we could call the command "python admin.py loaddata myfixture.json" in setUpClass() method. Refine the code like below:

from django.test import TestCase
from myapp.models import Animal
from django.core import management

class AnimalTestCase(TestCase):def setUpClass(cls):
        # use command to load fixture
        management.call_command("loaddata", "myfixture.json")
        call_setup_methods()

    def testFluffyAnimals(self):
        # A test that uses the fixtures.
        call_some_test_code()

If this project is not developed and tested in PyDev, it's okay now.

Otherwise there is another wired problem for fixture when you run the test case by clicking "run as unittest" in Eclipse PyDev, the fixture data doesn't work for other test method except setUpClass() in the testCase class. To make it work, you have to modify the code like below:

from django.test import TestCase
from myapp.models import Animal
from django.core import management

class AnimalTestCase(TestCase):
    fixtures = ['myfixture.json']

    def setUpClass(cls):
        # use command to load fixture
        management.call_command("loaddata", "myfixture.json")
        call_setup_methods()

    def testFluffyAnimals(self):
        # A test that uses the fixtures.
        call_some_test_code()

Not clear about the reason, maybe a little defect in PyDev, or just my misunderstand, any suggestion is welcome!

 

 

转载于:https://www.cnblogs.com/Alex-Python-Waiter/archive/2012/10/30/2747142.html

在信息化浪潮的推动下,数字政府平台建设成为提升政府治理能力和服务水平的关键举措。本方案旨在通过构建统一、高效、智能的数字政府平台,打破信息孤岛,实现政务服务的智能化、便捷化。方案首先明确了建设目标:打造智慧感知、智慧决策、智慧评价三位一体的数字政府体系。通过现代信息技术,全面感知用户需求,实现政务服务的精准推送;利用大数据分析技术,为政府决策提供科学依据;通过动态评估政策效果,及时调整优化服务策略。为实现这些目标,方案提出了集约化门户网站群、云协同办公平台、便民服务平台、一体化网格执法平台、大数据智能决策平台等一系列核心功能。这些平台不仅覆盖了政府日常办公、政务服务、社会治理等多个领域,还通过数据共享交换机制,实现了跨部门、跨系统的信息互联互通,极大地提高了政府工作的透明度和效率。在技术实现上,方案采用了基于J2EE的分布式应用结构设计、云计算虚拟化技术、可视化工作流引擎等先进技术,确保了平台的稳定性和可扩展性。同时,方案还注重用户体验,通过一体化信息展现、多渠道服务接入等方式,让市民和企业能够随时随地享受到便捷的政务服务。值得一提的是,方案中的大数据智能决策平台,通过挖掘分析海量政务数据,为政府提供了精准的预测预警和决策支持,让政府治理更加科学、智能。此外,为了保障平台的安全稳定运行,方案还从网络安全、数据安全、应用安全等多个层面制定了严密的安全策略。在实施层面,方案规划了详细的实施步骤和保障措施。通过分阶段实施、逐步推进的策略,确保项目能够按时按质完成。同时,方案还注重培训和支持,为政府工作人员提供了全面的技术培训和操作指南,帮助他们快速适应新的工作环境。随着数字政府平台的建成投用,政府将能够更加高效地服务市民和企业,推动经济社会持续健康发展。对于广大写方案的读者而言,本方案不仅提供了宝贵的实践经验和参考案例,还激发了对于未来数字政府建设的无限遐想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值