1. 为什么使用unitest?
主要用于自动化测试用例的开发和执行,这里的测试则是创建一个测试数据库,当正常结束时,会删掉测试数据库
2. 如何使用unittest
在
app
下新建test.py
文件-
导入
unittest
库import unittest
-
创建类继承
TestCase
类,eg.class GameTest(unittest.TestCase):
在类中定义
test
开头的方法,即可使用
3. 注意事项
必须要有
setUp
和tearDown
方法self.assertEqual
是TestCase
里断言的方法,类似于try except
每个测试用例必须要用
test
开头-
当出现
post
、get
、put
、delete
同时存在时,需要其间顺序delete put get post
执行顺序按照
ASCII
排序-
当使用
put
时,需要注意格式response = c.put(url,data=json.dumps({ "name": "tes1", "school": "实验高中" }),content_type='application/json') # 首先将参数json,其次要写上content_type='application/json'
-
当使用
django.contrib.auth.models
时,可以快捷给用户添加权限,参考代码from django.contrib.auth.models import Permission self.user = UserProfile.objects.create(username='teacher1') self.user.set_password('12345') self.user.save() pers = Permission.objects.filter(codename__in=['add_userinfo', 'change_userinfo', 'delete_userinfo']) for per in pers: self.user.user_permissions.add(per)
4. 具体使用
import unittest
from django.test import Client
from users.models import UserProfile, StudentProfile
from django.contrib.auth.models import Permission
class StudentTest(unittest.TestCase):
def setUp(self):
self.user = UserProfile.objects.create(username='teacher1')
self.user.set_password('12345')
self.user.save()
pers = Permission.objects.filter(codename__in=['add_studentprofile', 'change_studentprofile', 'delete_studentprofile'])
for per in pers:
self.user.user_permissions.add(per)
# put请求
def test_02_put_student(self):
c = Client()
user = UserProfile.objects.filter().first()
url = f'/api/student/{user.id}/'
c.login(username='teacher1', password='12345')
response = c.put(url,data=json.dumps({
"name": "小明",
"school": "实验高中"
}),content_type='application/json')
self.assertEqual(json.loads(response.content)["status"], 'ok')
c.logout()
# get请求
def test_03_get_student_list(self):
c = Client()
c.login(username='teacher1', password='12345')
response = c.get('/api/student/',{'page':1, 'page_size':2})
self.assertEqual(json.loads(response.content)["status"], 'ok')
c.logout()
# post请求
def test_04_post_student(self):
c = Client()
c.login(username='teacher1', password='12345')
response = c.post('/api/student/',{
"name": "小明",
"school": "实验高中"
})
self.assertEqual(json.loads(response.content)['status'], 'ok')
c.logout()
# get请求
def test_05_get_student_by_id(self):
c = Client()
c.login(username='teacher1', password='12345')
response = c.get('/api/student/',{'id':1})
self.assertEqual(json.loads(response.content)["status"], 'ok')
c.logout()
# delete请求
def test_01_delete_student(self):
c = Client()
c.login(username='teacher1', password='12345')
user = UserProfile.objects.filter().first()
url = f'/api/student/{user.id}/'
response = c.delete(url)
self.assertEqual(response.status_code, 204)
c.logout()
def tearDown(self):
self.user.delete()
5. 运行命令
#运行测试文件
python -m unittest test_module
#测试单个测试类
python -m unittest test_module.test_class
#测试多个测试类
python -m unittest test_module.test_class test_module2.test_class2
#通配符匹配测试文件执行
python -m unittest -p test*.py
#显示详细信息
python -m unittest -v test_module
#帮助
python -m unittest -h
#Django
python manage.py test