django test基本操作

来源:互联网 发布:php csdn博客 编辑:程序博客网 时间:2024/06/05 16:56

在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/',)
0 0
原创粉丝点击