单元测试

概念:

  • 单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。
  • 由于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'...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值