概念:
- 单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。
- 由于Django的特殊性,代码都放在类视图里,通过接口测试单元。
单元测试和接口测试的区别:
-
单元测试注重代码逻辑,接口测试注重业务逻辑
-
单元测试的粒度最小,是测试最小独立的单元模块(不依赖其他模块);接口测试不是,会覆盖很多
-
单元测试关注的是代码的实现和逻辑,测试范围较小,保证实现逻辑通过就行;接口测试因为关注业务,所以测试范围较广,会用更多的测试数据去测试
学习Django的单元测试
-
Django环境
数据库编码
数据库用户权限(需要临时建、删数据库) -
每个应用,自带tests.py
——类:继承Django.test.TestCase
——前置、后置方法
——test开头的测试用例 -
终端运行测试代码
进入manage.py目录,执行代码
测试指定测试文件:
python manage.py test <运行测试文件目录>或<文件>或<类>或<方法>
测试Django里所有tests.py文件:
python manage.py test
如果报错,则在后面加上-t .
-
Django单元测试的特点:
"""
django.test.TestCase类主要由前、后置处理方法 和test开头的方法组成
test开头的方法 是编写了测试逻辑的用例
setUp方法 (名字固定)在每一个测试方法执行之前被调用
tearDown方法(名字固定) 在每一个测试方法执行之前被调用
setUpClass类方法(名字固定)在整个类运行前执行只执行一次
tearDownClass类方法(名字固定)在调用整个类测试方法后执行一次
"""
from django.test import TestCase
class MyTest(TestCase):
@classmethod
def setUpClass(cls):
print('setUpClass')
@classmethod
def tearDownClass(cls):
print('tearDownClass')
def setUp(self) -> None:
print('setUp')
def tearDown(self) -> None:
print('tearDown')
def test_xxx(self):
print('测试用例1')
def test_yyy(self):
print('测试用例2')
测试结果:
(test_v7) python@ubuntu:~/Desktop/code/meiduo_mall$ python manage.py test meiduo_mall.apps.users.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
setUpClass
setUp
测试用例1
tearDown
.setUp
测试用例2
tearDown
.tearDownClass
----------------------------------------------------------------------
Ran 2 tests in 0.003s
OK
Destroying test database for alias 'default'...
- 编写测试代码示例:
import requests
from django.test import TestCase
class MyClassTestCase(TestCase):
s = None
@classmethod
def setUpClass(cls):
print('setUpClass')
info = {
"username": "admin",
"password": "chuanzhi",
"remembered": True
}
# 创建对象
cls.s = requests.Session()
# 登陆
resp = cls.s.post('http://127.0.0.1:8000/login/', json=info)
print('登陆结果:', resp.json())
@classmethod
def tearDownClass(cls):
print('tearDownClass')
resp = cls.s.delete('http://127.0.0.1:8000/logout/')
print('退出登陆结果:', resp.json())
def test_my_func(self):
"""测试功能"""
resp = self.s.get('http://127.0.0.1:8000/info/')
print('用户信息', resp.json())
def test_my_func1(self):
"""测试功能"""
resp = self.s.get('http://127.0.0.1:8000/browse_histories/')
print('浏览历史', resp.json())
def test_my_func2(self):
"""测试功能"""
resp = self.s.get('http://127.0.0.1:8000/addresses/')
print('用户地址信息', resp.json())
终端测试结果:
(test_v7) python@ubuntu:~/Desktop/code/meiduo_mall$ python manage.py test meiduo_mall.apps.users.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
setUpClass
登陆结果: {'errmsg': 'ok', 'code': 0}
用户信息 {'errmsg': 'ok', 'info_data': {'username': 'admin', 'mobile': '', 'email': '1@1.com', 'email_active': False}, 'code': 0}
.浏览历史 {'skus': [], 'errmsg': 'ok', 'code': 0}
.用户地址信息 {'default_address_id': None, 'errmsg': 'ok', 'addresses': [], 'code': 0}
.tearDownClass
退出登陆结果: {'errmsg': 'ok', 'code': 0}
----------------------------------------------------------------------
Ran 3 tests in 0.426s
OK
Destroying test database for alias 'default'...