django test基本操作

在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/',)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容