






















总所周知,django支持放在models.py中的doctest和tests.py,他们都要放在项目目录下。如果为了测试model, 使用doctest让测试挨着model的定义,而且也可方便得到文档,这可接受。但是tests.py放所有的测试案例严重不符合我的习惯,我喜欢把所有测试案例集中放在一个专门的目录中,而且一个文件一个TestCase。因此,考虑用suite()做一点工作,写一个tests.py:
" "" Build test suite with test cases under the folder mytests How to orgnize your tests cases: * put them in the folder mytests * name py file as *test.py, case-sensitivity ignored, eg. clientTest.py """ __author__ = 'zhangtao.it@gmail.com' import unittest,os,re TESTS = 'mytests' def suite(): app_path = __name__.split('.')[:-1] app_path.append(TESTS) path = os.path.abspath(os.path.dirname(__file__)) path = os.path.join(path, TESTS) files = os.listdir(path) test = re.compile("test\.py$", re.IGNORECASE) files = filter(test.search, files) #note: for importing, module name must contain the full
这就是我的tests.py, 把他放到app目录中,然后:
django测试是会生成专门一个测试数据库, 但里面没有数据,django自定义的TestCase提供了方法fixtures来加载测试数据到数据库。
python manage.py dumpdata auth.user --format json > yourappname/fixtures/user.json
测试案例
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。