python单元测试
文章目录
- 单元测试
- 定义
- 断言函数
- Test Fixtures
- Mock
- patch
- 装饰器模拟(首选)
- 上下文管理器模拟
- 手动模拟
- 测试实例
- 测试覆盖率
- pytest框架
- 起步
- 安装使用
- 常用参数
- 跳过测试
- @pytest.fixture
- conftest.py
- 参数化测试
- 数据库查询的mock
- 覆盖率
单元测试
定义
单元测试是指一个自动化的测试:
- 用来验证一小段代码的正确性
- 可以快速执行
- 在独立的环境中执行
断言函数
assertEqual
assertNotEqual
assertTrue
assertFalse
assertIs
assertIsNot
assertIsNone
assertIsNotNone
assertIn
assertNotIn
assertIsInstance
assertNotIsInstance
assertRaises
示例一:assertEqual
class Calculator:def add(self, *args):ret = 0for item in args:ret += itemreturn ret
from unittest import TestCasefrom server.app import Calculatorclass TestCalculator(TestCase):def test_add(self):calculator = Calculator()expect_result = 10actual_result = calculator.add(1, 2, 3, 4)self.assertEqual(expect_result, actual_result)
示例二:assertRaises
class Service:def download_img(self, url: str):if url:return Trueraise ValueError("url error")
from unittest import TestCase
from server.app import Serviceclass TestService(TestCase):def test_download_img_success(self):service = Service()ret = service.download_img("http://www.baidu.com/1.png")self.assertTrue(ret)def test_download_img_with_exception(self):service = Service()with self.assertRaises(ValueError):service.download_img("")
Test Fixtures
在测试方法执行之前或者之后执行的函数或者方法被称为Test Fixtures
- module级别的Fixtures:
setUpModule,tearDownModule - class级别的Fixtures:
setUpClass,tearDownClass - method级别的Fixtures:
setUp,tearDown
class Service:def download_img(self, url: str):if url:return Trueraise ValueError("url error")
from unittest import TestCase
from server.app import Servicedef setUpModule():print("执行module前...")def tearDownModule():print("执行module后...")class TestService(TestCase):@classmethoddef setUpClass(cls):print("执行class前...")@classmethoddef tearDownClass(cls):print("执行class后...")def setUp(self):self.service = Service()print("执行任意测试方法前...")def tearDown(self):print("执行任意测试方法后...")def test_download_img_success(self):ret = self.service.download_img("http://www.baidu.com/1.png")self.assertTrue(ret)def test_download_img_with_exception(self):with self.assertRaises(ValueError):self.service.download_img("")
执行module前...
执行class前...
执行任意测试方法前...
执行任意测试方法后...
执行任意测试方法前...
执行任意测试方法后...
执行class后...
执行module后...
Mock
模拟函数,方法,类的行为。
-
Mock:主要模拟指定的方法和属性
-
MagicMock:Mock的子类,同时模拟了很多Magic方法(
__len__,__str__方法等)
示例一:
from unittest.mock import Mockdef test_hello():hello = Mock()hello.find_user.return_value = {'name': '旺财','age': 1}print(hello.find_user())if __name__ == '__main__':test_hello()
{'name': '旺财', 'age': 1}
示例二:
class Student:def __init__(self, id: int, name: str):self.id = idself.name = namedef find_name_by_id(id):passdef save_student(student):passdef chang_name(id: int, new_name: str):student = find_name_by_id(id)if student:student.name = new_namesave_student(student)
from unittest.mock import Mock
from unittest import TestCase
from server.app import chang_name
from server import appclass TestService(TestCase):def test_change_name_with_record(self):service.find_name_by_id = Mock()student = Mock(id=1, name='旧名字')service.find_name_by_id.return_value = studentservice.save_student = Mock()chang_name(1, '新名字')self.assertEqual('新名字', student.name)service.find_name_by_id.assert_called()service.save_student.assert_called()def test_change_name_without_record(self):service.find_name_by_id = Mock()service.find_name_by_id.return_value = Noneservice.save_student = Mock()chang_name(1, '新名字')# 断言没有被调用service.save_student.assert_not_called()
patch
path可以临时用Mock对象替换一个目标(函数,方法,类)。本质还是上一节的Mock操作。
path可以替换的目标:
- 目标必须是可import的
- 是在使用的目标的地方替换而不是替换定义
path的使用方式:
- 装饰器的方式
- 上下文管理器的方式
- 手动方式
装饰器模拟(首选)
class Student:def __init__(self, id: int, name: str):self.id = idself.name = namedef find_name_by_id(id):passdef save_student(student):passdef chang_name(id: int, new_name: str):student = find_name_by_id(id)if student:student.name = new_namesave_student(student)
from unittest.mock import Mock, patch
from unittest import TestCase
from server.app import chang_nameclass TestService(TestCase):@patch("server.server.save_student")@patch("server.server.find_name_by_id")def test_change_name_decorator(self, find_name_by_id_mock, save_student_mock):student = Mock(id=1, name='旧名字')find_name_by_id_mock.return_value = studentchang_name(1, '新名字')self.assertEqual('新名字', student.name)find_name_by_id_mock.assert_called()save_student_mock.assert_called()
上下文管理器模拟
from unittest.mock import Mock, patch
from unittest import TestCase
from server.app import chang_nameclass TestService(TestCase):def test_change_name_context(self):student = Mock(id=1, name='旧名字')with patch("server.server.find_name_by_id") as find_name_by_id_mock, patch("server.server.save_student"):find_name_by_id_mock.return_value = studentchang_name(1, '新名字')self.assertEqual('新名字', student.name)
手动模拟
from unittest.mock import Mock, patch
from unittest import TestCase
from server.app import chang_nameclass TestService(TestCase):@patch("server.server.find_name_by_id")def test_change_name_manual(self, find_name_by_id_mock):student = Mock(id=1, name='旧名字')find_name_by_id_mock.return_value = studentpather = patch("server.server.save_student")pather.start()chang_name(1, '新名字')pather.start()self.assertEqual('新名字', student.name)
测试实例
path里面的模拟对象已经对所有魔术方法都进行了mock,如果不关心返回值可以不用后续return_value了
import os.path
from urllib.request import urlopen, Requestdef download_img(url: str):site_url = Request(url, headers={"User-Agent": "Mozilla/5.0"})with urlopen(site_url) as web_file:img_data = web_file.read()if not img_data:raise Exception(f"Error: cannot load the image from {url}")file_name = os.path.basename(url)with open(file_name, 'wb') as file:file.write(img_data)return f"Download image successfully, {file_name}"
from unittest.mock import patch, MagicMock
from unittest import TestCase
from server.app import download_img# https://www.bilibili.com/video/BV1EK411B7LX/?spm_id_from=333.788&vd_source=35b478ef20f153fb3c729ee792cdf651
class TestService(TestCase):# urlopen在方法参数中被mock为urlopen_mock# urlopen_mock的返回值是一个urlopen_result_mock# urlopen_result_mock的__enter__方法返回值是一个web_file_mock# web_file_mock的read方法返回值需要定义@patch("server.server.urlopen")# 因为在service.service文件中引入了,所以可以直接使用service.service引入Request@patch("server.server.Request.__new__")def test_download_img_with_exception(self, request_init_mock, urlopen_mock):# Setupurl = 'https://www.google.com/a.png'urlopen_result_mock = MagicMock()web_file_mock = MagicMock()urlopen_mock.return_value = urlopen_result_mockurlopen_result_mock.__enter__.return_value = web_file_mockweb_file_mock.read.return_value = Nonewith self.assertRaises(Exception):download_img(url)@patch("builtins.open")@patch("os.path.basename")@patch("server.server.urlopen")@patch("server.server.Request.__new__")def test_download_img_with_success(self, request_init_mock, urlopen_mock, basename_mock, open_mock):# Setupurl = 'https://www.google.com/a.png'urlopen_result_mock = MagicMock()web_file_mock = MagicMock()urlopen_mock.return_value = urlopen_result_mockurlopen_result_mock.__enter__.return_value = web_file_mockweb_file_mock.read.return_value = 'not none'basename_mock.return_value = 'fff'ret = download_img(url)self.assertEqual("Download image successfully, fff", ret)
测试覆盖率
#统计测试覆盖率
python -m coverage run -m unittest#查看覆盖率报告
python -m coverage report#生成html格式的覆盖率报告
python -m coverage html
pytest框架
起步
pytest是一个基于python语言的第三方测试框架。
有以下优点:
- 语法简单
- 自动检测测试代码
- 跳过指定测试
- 开源
安装使用
#安装
pip install pytest#运行(自动查找test_*.py,*_test.py测试文件。自动查找测试文件中test_开头的函数和Test开头的类中的test_开头的方法)
pytest
pytest -v#测试指定测试类
pytest test_xxx.py
常用参数
-v 输出详细的执行信息,比如文件和用例名称
-s 输出调试信息,比如print的打印信息
-x 遇到错误用例立即停止
跳过测试
@pytest.mark.skip
@pytest.mark.skipif
import sysfrom server.app import Student
import pytestdef skip():return sys.platform.casefold() == 'win32'.casefold()# @pytest.mark.skip(reason="暂时跳过")
@pytest.mark.skipif(condition=skip(), reason="window平台跳过")
class TestStudent:def test_student_create(self):student = Student(1, 'bob')assert student.id == 1assert student.name == 'bob'def test_student_create():student = Student(2, 'alice')assert student.id == 2assert student.name == 'alice'
@pytest.fixture
class Student():def __init__(self, id: int, name: str):self.id = idself.name = namedef valid_name(self):if self.name:return 3 < len(self.name) < 10return False
from server.app import Student
import pytest@pytest.fixture
def valid_student():student = Student(1, 'Kite')yield student@pytest.fixture
def not_valid_student1():student = Student(2, 'abcdefjijklmnopq')yield student@pytest.fixture
def not_valid_student2(not_valid_student1):# 这里不能对valid_student的name进行赋值修改哟student = Student(3, 'Bob')student.name = not_valid_student1.nameyield studentdef test_student(valid_student, not_valid_student1, not_valid_student2):ret = valid_student.valid_name()assert retret = not_valid_student1.valid_name()assert not retret = not_valid_student2.valid_name()assert not ret
conftest.py
作用:使得fixture可以被多个文件中的测试用例复用。
在tests目录下建立conftest.py文件,这里引入其他文件中的fixture,那么其他用例中就可以使用这些fixture,你也可以定义fixture在这个文件中(但是不推荐哈)
参数化测试
# 判断是否是奇数
def is_odd(x: int):return x % 2 != 0
import pytestfrom server.app import is_odd@pytest.mark.parametrize("num,expect_ret", [(1, True), (2, False)])
def test_is_odd(num, expect_ret):actual_ret = is_odd(num)assert expect_ret == actual_ret
数据库查询的mock
import pytest
from unittest.mock import patch, MagicMock
from server.controller.message_controller import create_user@pytest.fixture
def mock_session_scope():with patch("server.db.session.session_scope") as mock_session_scope:mock_session_scope_return_value = MagicMock()mock_session_scope.return_value = mock_session_scope_return_valuesession_mock = MagicMock()mock_session_scope_return_value.__enter__.return_value = session_mockyield session_mockdef test_create_user(mock_session_scope):ret = create_user("alice")assert 'ok' == retdef test_create_user_exception(mock_session_scope):with pytest.raises(ValueError):create_user("")
覆盖率
pip install pytest
pip install pytest-cov
pytest --cov --cov-report=html
相关文章:
python单元测试
文章目录 单元测试定义断言函数Test FixturesMockpatch装饰器模拟(首选)上下文管理器模拟手动模拟 测试实例 测试覆盖率pytest框架起步安装使用常用参数跳过测试pytest.fixtureconftest.py参数化测试 数据库查询的mock覆盖率 单元测试 定义 单元测试是…...
华为---静态路由-浮动静态路由及负载均衡(二)
7.2 浮动静态路由及负载均衡 7.2.1 原理概述 浮动静态路由(Floating Static Route)是一种特殊的静态路由,通过配置去往相同的目的网段,但优先级不同的静态路由,以保证在网络中优先级较高的路由,即主路由失效的情况下,…...
Maven deploy上传远程私服失败
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project 你的项目: Cannot deploy artifacts when Maven is in offline mode 解决方案: 1.IDEA把这个钩子去掉 2. settings.xml里把 <offline>标…...
通天星CMSV6车载定位监控平台 point_manage/merge SQL注入致RCE漏洞复现
0x01 产品简介 通天星CMSV6车载定位监控平台拥有以位置服务、无线3G/4G视频传输、云存储服务为核心的研发团队,专注于为定位、无线视频终端产品提供平台服务,通天星CMSV6产品覆盖车载录像机、单兵录像机、网络监控摄像机、行驶记录仪等产品的视频综合平台。 0x02 漏洞概述 …...
图像识别技术在人脸识别领域的新突破
图像识别技术在人脸识别领域的新突破主要体现在多个方面,这些突破不仅提高了人脸识别的准确性和效率,还拓展了其应用领域。以下是对这些新突破的详细归纳: 深度学习技术的应用: 深度学习技术,特别是卷积神经网络&…...
iview 组件里面的(任何一个月)整月日期全部选中_iview时间轴选中有历史记录日期
iview 组件里面的整月日期全部选中: ①:第一种是当前月的日期全部选中: 先上效果图:当前月分 获取到的值: 当前月的方法: // getDateStr() {// var curDate new Date();// var curMonth curDate.ge…...
Charles配置与API数据抓取
2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板)-CSDN博客跳槽涨薪的朋友们有福了,今天给大家推荐一个软件测试面试的刷题小程序。https://blog.c…...
[FreeRTOS 内部实现] 信号量
文章目录 基础知识创建信号量获取信号量释放信号量信号量 内部实现框图 基础知识 [FreeRTOS 基础知识] 信号量 概念 创建信号量 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( ( uint8_t ) 3U ) #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( uint8_t ) 0U ) #define xSe…...
Vue57-组件的自定义事件_解绑
给谁绑的自定义事件,就找谁去触发;给谁绑的自定义事件,就找谁去解绑; 一、解绑自定义事件 1-1、解绑一个自定义事件 到student.vue组件中去解绑。 1-2、解绑多个自定义事件 使用数组来解绑多个。 1-3、解绑所有的自定义事件 二、…...
Java启动jar设置内存分配详解
在微服务架构越来越盛行的情况下,我们通常一个系统都会拆成很多个小的服务,但是最终部署的时候又因为没有那么多服务器只能把多个服务部署在同一台服务器上,这个时候问题就来了,服务器内存不够,这个时候我们就需要对每…...
Feign Client超时时间设置不生效问题
在使用Feign Client时,可以通过两种方式来设置超时时间: 针对整个Feign Client设置超时时间 可以在Feign Client的配置类中通过修改Request.Options对象来设置超时时间。Request.Options对象有两个属性,connectTimeoutMillis用于设置连接超…...
Haproxy部署Web群集
概论 HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,是免费、快速并且可靠的一种解决方案。HAProxy非常适用于并发大(并发达1w以上)web站点,这些站点通常又需要会话保持或七层处理。HAProxy的运行模式使得它可以…...
C++STL梳理
CSTL标准手册: https://cplusplus.com/reference/stl/ https://cplusplus.com/reference/vector/vector/at/ 1、STL基础 1.1、STL基本组成(6大组件13个头文件) 通常认为,STL 是由容器、算法、迭代器、函数对象、适配器、内存分配器这 6 部分构成&…...
找出1000以内的所有的完数
完数的概念:完数(Perfect Number)是一个正整数,它等于除了它本身以外所有正因子之和。例如,6的因子有1、2、3和6,其中1236,所以6是一个完数。 #include <stdio.h> // 函数用于计算一个数…...
3110. 字符串的分数
给你一个字符串 s 。一个字符串的 分数 定义为相邻字符 ASCII 码差值绝对值的和。 请你返回 s 的 分数 。 示例 1: 输入:s "hello" 输出:13 解释: s 中字符的 ASCII 码分别为:h 104 ,e 1…...
Mybatis MySQL allowMultiQueries 一次性执行多条语句
在JDBC 增加参数allowMultiQueries jdbc:mysql://localhost:3306/abc?&allowMultiQueriestrue <insert id"addRi" parameterType"java.util.List">DELETE FROM sys_ri WHERE sr_id #{roId} AND sr_fion_id #{fod};INSERT into sys_rVALUES&…...
Kubernates容器化JVM调优笔记(内存篇)
Kubernates容器化JVM调优笔记(内存篇) 先说结论背景思路方案 先说结论 1、首先如果是JDK8,需要使用JDK8_191版本以上,才支持容器化环境和以下参数,否则就更新到JDK10以上,选择对应的镜像构建就行了 2、在容…...
Elasticsearch Scroll 报错entity content is too long
2024-06-24 15:22:01:568 ERROR [task-31] (ScrollFetcherProduceAction.java:129) 访问ES出错org.apache.http.ContentTooLongException: entity content is too long [112750110] for the configured buffer limit [104857600]at org.elasticsearch.client.HeapBufferedAsync…...
Vue iview输入框change事件replace正则替换不生效问题的解决。
// 需求:输入座机号只允许输入数字和"-" onChange(e){this.$nextTick(()>{this.phone e.target.value.replace(/[^0-9-]/g, );}) } 解决:添加**this.$nextTick**即可...
Prestashop跨境电商独立站,外贸B2C网站完整教程
Prestashop是一款来自法国专业的开源电商CMS(内容管理系统)平台,和wordpress一样比较轻量,适合中小网站。Prestashop跨境电商独立站在国内并不是很流行,不过国外是非常火的,从各大平台的Prestashop主题数量就可以看得出来。 最有…...
内存分配函数malloc kmalloc vmalloc
内存分配函数malloc kmalloc vmalloc malloc实现步骤: 1)请求大小调整:首先,malloc 需要调整用户请求的大小,以适应内部数据结构(例如,可能需要存储额外的元数据)。通常,这包括对齐调整,确保分配的内存地址满足特定硬件要求(如对齐到8字节或16字节边界)。 2)空闲…...
Objective-C常用命名规范总结
【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名(Class Name)2.协议名(Protocol Name)3.方法名(Method Name)4.属性名(Property Name)5.局部变量/实例变量(Local / Instance Variables&…...
el-switch文字内置
el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...
pikachu靶场通关笔记22-1 SQL注入05-1-insert注入(报错法)
目录 一、SQL注入 二、insert注入 三、报错型注入 四、updatexml函数 五、源码审计 六、insert渗透实战 1、渗透准备 2、获取数据库名database 3、获取表名table 4、获取列名column 5、获取字段 本系列为通过《pikachu靶场通关笔记》的SQL注入关卡(共10关࿰…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...
RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程
本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在聚客AI学院。 本文全面剖析RNN核心原理,深入讲解梯度消失/爆炸问题,并通过LSTM/GRU结构实现解决方案,提供时间序列预测和文本生成…...
AI书签管理工具开发全记录(十九):嵌入资源处理
1.前言 📝 在上一篇文章中,我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源,方便后续将资源打包到一个可执行文件中。 2.embed介绍 🎯 Go 1.16 引入了革命性的 embed 包,彻底改变了静态资源管理的…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
以光量子为例,详解量子获取方式
光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学(silicon photonics)的光波导(optical waveguide)芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中,光既是波又是粒子。光子本…...
HashMap中的put方法执行流程(流程图)
1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中,其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下: 初始判断与哈希计算: 首先,putVal 方法会检查当前的 table(也就…...
