在tests.py中,导入TestCase包

from django.test import TestCase

创建测试类:

class SimpleTest(TestCase):

创建测试函数,必须以test_开头

def test_login(self):

模拟客户端发送数据

c=Client(HTTP_USER_AGENT='Mozilla/5.0') 
response=c.post('/user/login/',{'username':'admin','password':'admin'})

检查返回数据

self.assertEqual(response.status_code, 200)

运行

python manage.py test <app name>

在测试中,加载预数据

导出原有数据库的数据

python manage.py dumpdata > fixtures/admin.json

在测试类中加载

fixtures = ['admin.json']

在setting中设置fixtures的绝对路径

FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
posted on 2017-05-05 15:06  HeJD  阅读(312)  评论(0编辑  收藏  举报