一、继承TestCase类
在Django工程子应用的tests.py
文件中编写测试代码:
"""
1. 注册
a. 发送图片验证玛
b. 发送短信验证码,需要上一步图片验证码(直接读redis)
c. 注册(手机验证为上一步,验证码固定为123456)
2. 登陆
3. 获取登陆用户
4. 登出
5. 获取登陆用户
"""
from django.test import TestCase
import requests
from django_redis import get_redis_connection
class MyTest(TestCase):
def setUp(self) -> None:
print('setUp')
# 创建一个session对象,实例属性
self.s = requests.Session()
def tearDown(self) -> None:
print('tearDown')
del self.s # 删除变量,不是必须,这里只是为了匹配
def test_xxx(self):
# self.do_register()
self.do_login()
self.do_info()
self.do_logout()
self.do_info()
def do_register(self):
"""注册"""
print('注册')
_uuid = '0fdb75a8-d60e-4598-86bc-c3e7991aadf9'
# a.发送图片验证玛
# http://域名或ip:端口/image_codes/0fdb75a8-d60e-4598-86bc-c3e7991aadf9/
self.s.get(f'http://localhost:8000/image_codes/{_uuid}/')
# b.发送短信验证码,需要上一步图片验证码(直接读redis)
# 从redis读取图片验证码
# 1.2 读取redis中的图形验证码
conn = get_redis_connection('verify_code')
# redis返回的数据,如果没有则None,如果有返回的是字符串的字节形式b'CFGR' --> b'CFGR'.decode() --> 'CFGR'
image_code_from_redis = conn.get('img_%s' % _uuid)
image_code = image_code_from_redis.decode()
print(image_code)
mobile = "13377778888"
# http://域名或ip:端口/sms_codes/任意手机号/?image_code=上一步获取的验证码&image_code_id=0fdb75a8-d60e-4598-86bc-c3e7991aadf9
r = self.s.get(f'http://localhost:8000/sms_codes/{mobile}/?image_code={image_code}&image_code_id={_uuid}')
print('短信验证码结果 = ', r.json())
# c.注册(手机验证为上一步,验证码固定为123456)
info = {
"username":"mikeabc",
"password":"chuanzhi12345",
"password2":"chuanzhi12345",
"allow":True,
"mobile":mobile,
"sms_code":"123456"
}
r = self.s.post('http://localhost:8000/register/', json=info)
print('注册结果 = ', r.json())
def do_login(self):
info = {
"username":"mikeabc",
"password":"chuanzhi12345",
"remembered":True,
}
r = self.s.post('http://localhost:8000/login/', json=info)
print('登陆结果 = ', r.json())
def do_info(self):
r = self.s.get('http://localhost:8000/info/')
print('用户信息结果 = ', r.json())
def do_logout(self):
r = self.s.delete('http://localhost:8000/logout/')
print('登出结果 = ', r.json())
终端运行测试代码:
manage.py目录下:python manage.py test <测试代码文件>
python manage.py test meiduo_mall.apps.users.tests
二、 Client的使用
-
特点
-
可以状态保存
-
只能使用在django中
-
不需要启动Django服务,使用的是临时的数据库
-
发送post请求,只需要路径’/login’, 不需要url
- 如果发送json格式,需要指定数据格式
content_type='application/json'
- 如果发送json格式,需要指定数据格式
-
测试由于是走临时数据库(mysql),注册完账号就删除了。所以,注册、登陆是一起测试的。
-
测试代码示例:
"""
1. 注册
a. 发送图片验证玛
b. 发送短信验证码,需要上一步图片验证码(直接读redis)
c. 注册(手机验证为上一步,验证码固定为123456)
2. 登陆
3. 获取登陆用户
4. 登出
5. 获取登陆用户
"""
from django.test import TestCase, Client
import requests
from django_redis import get_redis_connection
class MyTest(TestCase):
def setUp(self) -> None:
print('setUp')
# 创建一个Client()对象,实例属性
self.s = Client()
def tearDown(self) -> None:
print('tearDown')
del self.s # 删除变量,不是必须,这里只是为了匹配
def test_xxx(self):
self.do_register()
self.do_login()
self.do_info()
self.do_logout()
self.do_info()
def do_register(self):
"""注册"""
print('注册')
_uuid = '0fdb75a8-d60e-4598-86bc-c3e7991aadf9'
# a.发送图片验证玛
# http://域名或ip:端口/image_codes/0fdb75a8-d60e-4598-86bc-c3e7991aadf9/
self.s.get(f'/image_codes/{_uuid}/')
# b.发送短信验证码,需要上一步图片验证码(直接读redis)
# 从redis读取图片验证码
# 1.2 读取redis中的图形验证码
conn = get_redis_connection('verify_code')
# redis返回的数据,如果没有则None,如果有返回的是字符串的字节形式b'CFGR' --> b'CFGR'.decode() --> 'CFGR'
image_code_from_redis = conn.get('img_%s' % _uuid)
image_code = image_code_from_redis.decode()
print(image_code)
mobile = "13377778888"
# http://域名或ip:端口/sms_codes/任意手机号/?image_code=上一步获取的验证码&image_code_id=0fdb75a8-d60e-4598-86bc-c3e7991aadf9
r = self.s.get(f'/sms_codes/{mobile}/?image_code={image_code}&image_code_id={_uuid}')
print('短信验证码结果 = ', r.json())
# c.注册(手机验证为上一步,验证码固定为123456)
info = {
"username":"mikemike",
"password":"chuanzhi12345",
"password2":"chuanzhi12345",
"allow":True,
"mobile":mobile,
"sms_code":"123456"
}
# client.post只有data关键字参数,需要通过content_type='application/json'指定请求体为json格式
r = self.s.post('/register/', data=info, content_type='application/json')
print('注册结果 = ', r.json())
def do_login(self):
info = {
"username":"mikemike",
"password":"chuanzhi12345",
"remembered":True,
}
r = self.s.post('/login/', data=info, content_type='application/json')
print('登陆结果 = ', r.json())
def do_info(self):
r = self.s.get('/info/')
print('用户信息结果 = ', r.json())
def do_logout(self):
r = self.s.delete('/logout/')
print('登出结果 = ', r.json())
三、断言
概念:对预期和实际的结果进行比对,比对的结果是真或假,真表示成功,假表示失败。一般情况下,如果是失败的,还应该给出失败的原因。
断言方法 | 断言描述 |
---|---|
assertEqual(arg1, arg2, msg=None) | 预期arg1=arg2,不等则fail |
assertNotEqual(arg1, arg2, msg=None) | 预期arg1 != arg2, 相等则fail |
assertTrue(expr, msg=None) | 预期expr是true,如果为false,则fail |
assertFalse(expr, msg=None) | 预期expr是false,如果为true,则fail |
assertIsNone(expr, msg=None) | 预期expr是None,不是则fail |
assertIsNotNone(expr, msg=None) | 预期expr不是None,是则fail |
assertIn(arg1, arg2, msg=None) | 预期arg1在arg2中,不是则fail |
回到继承TestCase类的方法,当我们进行测试登录接口的时候,最后使用断言
的方法进行判断是否测试成功。
from django.test import TestCase
import requests
class MyTest(TestCase):
def setUp(self) -> None:
print('setUp')
# 创建一个session对象,实例属性
self.s = requests.Session()
def tearDown(self) -> None:
print('tearDown')
del self.s # 删除变量,不是必须,这里只是为了匹配
def test_xxx(self):
info = {
"username":"mikeabc11",
"password":"chuanzhi12345",
"remembered":True,
}
r = self.s.post('http://localhost:8000/login/', json=info)
print('登陆结果 = ', r.json())
ret = r.json()
# 断言:预期结果和实际结果是否符合
# 如果ret['code']和0相等,说明符合预期,测试通过,断言成功,msg的信息不会显示
# 如果ret['code']和0不相等,说明不符合预期,测试失败,断言失败,msg的信息会显示
# 断言失败,标志为F,抛出异常
self.assertEqual(ret['code'], 0, msg=ret['errmsg'])
终端运行测试: