惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
S
Security Affairs
W
WeLiveSecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
Scott Helme
Scott Helme
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
U
Unit 42
爱范儿
爱范儿
腾讯CDC
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Y
Y Combinator Blog
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
IT之家
IT之家
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 聂微东
Cloudbric
Cloudbric
V
V2EX
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
TaoSecurity Blog
TaoSecurity Blog
T
Tor Project blog
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
MyScale Blog
MyScale Blog

博客园 - 观无明

IE9 + django开发版WEB服务器 不响应或Socket报错 google chrome 下django用户登录失败的问题 django 开发多语言网站 不愁男女比例不协调 Word文件如源码一样也可比较和合并 python 的字符编码和中文处理 - 观无明 - 博客园 my django development environment (virtualenv+pip+django) nginx+fastcgi+django实践笔记 django 开发 - 小心模板文件的编码格式(utf-8) 汉王电子书D20使用笔记 使用south实现Django的数据库升级迁移 VIM 笔记 (for python ) 在Windows上使用Linux命令 cygwin 仍有人在真心关注这灾难 CruiseControl中应用NCover和NCoverExplore CruiseControl中使用NUnit中测试WEB服务 Resharper封装(Encapsulate)域Field为属性Property的命名问题 数据库开发的持续集成 - CruiseControl.Net的项目配置 数据库开发的持续集成 - Liquibase的简介和应用
djang 测试心得
观无明 · 2010-05-13 · via 博客园 - 观无明

突破test.py,弄个目录放测试案例

总所周知,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目录中,然后:

  • app目录中建一个目录mytests, 用来放你的测试案例。记得搞一个空的__init__.py
  • 注意所有测试案例py文件命名为*test.py, 处理是忽略大小写。

如何准备测试数据

django测试是会生成专门一个测试数据库, 但里面没有数据,django自定义的TestCase提供了方法fixtures来加载测试数据到数据库。

  • 使用manage.py dumpdata 把正式数据库的数据导出来,保存在json文件中(如user.json),放在APP下fixtures目录里,如例

python manage.py  dumpdata auth.user --format json > yourappname/fixtures/user.json

  • 如此写测试案例,django会在setUp之后加载fixtures,把数据填充到数据库。当然,你也可自己编写json文件喂给它。
测试案例