Fixtures在Django测试中的使用
来源:互联网 发布:最小公倍数算法流程图 编辑:程序博客网 时间:2024/05/02 00:21
在对Django项目做单元测试时,经常需要生成或者导入一些初始数据。对于已经有数据存在于正式数据库的app来说,使用Fixture 载入数据,是最简便有效的方法。
阅读全文
首先,你需要一份json格式的数据文件,Django对此提供了很好的支持—dumpdata 命令
"python manage.py dumpdata myapp >myapp/fixtures/myapp.json"
这个命令将帮助你把数据库中myapp 导入到myapp/fixtures/myapp.json中。
然后在setting.py中加入:
FIXTURE_DIRS = ('/path/to/api/fixtures/',)
接着在test.py中 加入:
fixtures = ['myapp.json']
最后么当然是运行test命令了:
./manage.py test myapp
下面贴出一个测试用例的完整代码:
class TestFilterDeleteBiz(TestCase): fixtures = ['bizs.json'] def setUp(self): pass def test_tolist_should_equal_filterdbizs(self): delete_bizs = SETTINGS.DELETE_BIZCHANNEL bizs = BizChannel.objects.exclude(name__in=delete_bizs) append_user(bizs) check_email_addrs = [ b.user['email'] for b in bizs] to_list = get_TO_list() is_equal = to_list == check_email_addrs self.assertTrue(is_equal, "sorry ")
0 0
- Fixtures在Django测试中的使用
- maplotlib在django中的使用
- Python 中的 fixture 和 fixtures
- Python的Descriptor在Django中的使用
- fiddler在测试中的使用
- JavaScript中的模板(jsRender)在Django中的使用
- 在Django所有模板中使用session中的数据
- 异步任务队列Celery在Django中的使用
- Django中的forloop使用
- python在自动化测试中的使用
- Btrace在性能测试中的使用
- iperf工具在网络测试中的使用
- HtmlUnit在本地化测试中的使用
- Django在apache中的配置
- Yii框架中使用Fixtures的一个大坑。。
- Yii框架的单元测试中使用Fixtures详解
- 在Centos搭建Django测试环境
- Django框架中的Context使用
- 闲评好声音那英及杨坤组决赛
- 5款备受关注的开源NoSQL数据库
- 程序员求职之道(《程序员面试笔试宝典》)之海量数据处理(排序问题)?
- 网易2012校园招聘笔试题目
- Check if a binary tree is subtree of another binary tree
- Fixtures在Django测试中的使用
- If you are given two traversal sequences, can you construct the binary tree?
- Map-Reduce: Shuffle and Sort
- 【gcd+数学证明】【HDU1722】 CAKE
- javaScript中的事件对象event
- Stanford机器学习---第一讲. Linear Regression with one variable
- 为你的rm加个回收站(ubuntu)
- 112 - Tree Summing
- No orientation specified, and the default is horizontal. This is a common source of bugs when childr