当前位置:网站首页>pytest.mark.parametrize及mock使用
pytest.mark.parametrize及mock使用
2022-07-25 13:32:00 【木下瞳】
目录
pytest.mark.parametrize
现在有 zjk.py 模块,想测试其中 add 函数的功能,传入 x, y,把 x 平方后的结果与 y 相加返回。
zjk.py:
def power(x):
return x ** 2
def add(x ,y):
x = power(x)
return x + y创建一个测试文件,测试文件一般以 test 开头或结尾,创建 test_zjk.py:
import pytest
from zjk import add
@pytest.mark.parametrize("pow_res, x, y, excepts", [
(4, 2, 5, 9),
(16, 4, 11, 27)
])
def test_add(pow_res, x, y, excepts):
res = add(x ,y)
assert res == excepts在 pycharm 中可以单独运行测个测试函数:

运行结果没有报错,说明测试用例全都通过:

如果测试用例失败不符合预期结果,会报错提示是哪个用例不通过:
@pytest.mark.parametrize
这个是参数模拟设置用的函数,在此例中的测试函数传入了 4 个参数,其中 x,y 是必要的,其他两个是自己加的,因为 x,y 是要测的传入的值,另外两个一个是 x 平方后的值,一个是预期输出值,对应关系,这里是两组测试用例,及代表要运行 2 次:

调用 add 函数得到计算结果,关键字 assert 断言,判断结果与预期结果等不等,不等为 FAlse,会不通过用例,即上面的截图。
mock.patch
看上面的 zjk.py 里面 add 函数是调用了一个 power 函数的,而 add 是想测的函数,我们只想测这个函数的代码,中途调用的函数过程不想关心,想直接用它返回的值,因为万一 power 函数是一些网络请求,cmd 命令等,就需要网络,机器等,所以为了专注测试,用 patch 模拟 power 函数返回值
zjk.py 我们在其中再添加一个函数:
def power(x):
return x ** 2
def sub(y):
return y - 1
def add(x ,y):
x = power(x)
y = sub(y)
return x + ytest.py 修改:
import mock
import pytest
from zjk import add
@pytest.mark.parametrize("x, y, excepts, pow_res, sub_res", [
(2, 5, 8, 4, 4),
(3, 4, 12, 9, 3),
(7, 7, 4, [1], 3)
])
@mock.patch("zjk.sub")
@mock.patch("zjk.power")
def test_add(mock_power, mock_sub, x, y, excepts, pow_res, sub_res):
if isinstance(pow_res, list):
mock_power.side_effect = pow_res
else:
mock_power.return_value = pow_res
mock_sub.return_value = sub_res
res = add(x ,y)
assert res == excepts添加了 @mock.patch,代表要测的 add 函数中间过程调用的函数,如 zjk.sub,这里表示传入的是sub 函数路径,之后需要在测试函数 test_add 传入函数,格式为 mock_函数名,且有顺序要求,传入参数顺序(左往右),装饰器顺序(下往上)
parametrize 中也需要添加 power,sub 函数的取值,就可以测试了
这里用了 if else 是为了说明 side_effect,return_value
return_value 好理解就是设定返回值,如 mock_power.return_value = pow_res 把 power 函数的返回值进行赋值,但必须为数值型或字符串
side_effect 赋值的要求必须是可迭代对象,所以在用例中有一条我取了列表
根据实际情况进行设置,用反了会报错
来看一下结果:

边栏推荐
- 【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
- Hcip eighth day experiment
- 2022全球开发者中,你的收入排多少?
- Convolutional neural network model -- vgg-16 network structure and code implementation
- Numpy简介和特点(一)
- Uncaught SyntaxError: Octal literals are not allowed in strict mode.
- hcip第七天笔记
- 刷题-洛谷-P1146 硬币翻转
- Basic knowledge of binary tree
- Shell common script: check whether a domain name and IP address are connected
猜你喜欢

音视频技术开发周刊 | 255

Excel录制宏

Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building

领域驱动模型设计与微服务架构落地-模型设计

Discussion on principle and application technology of MLIR
![[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)](/img/d2/6be99fd194c66e4f60af38c6e52c93.png)
[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)

从输入网址到网页显示

Django 2 ----- 数据库与Admin

How to solve the problem of taking up too much space when recording and editing videos?

Shell common script: judge whether the file of the remote host exists
随机推荐
为提高效率使用ParallelStream竟出现各种问题
ES6数组去重 new Set()
电脑里一辈子都不想删的神仙软件
一味地做大元宇宙的规模,已经背离了元宇宙本该有的发展逻辑
C#基础学习(二十三)_窗体与事件
【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
Hcip day 9 notes
0715RHCSA
Generate SQL script file by initializing the latest warehousing time of vehicle attributes
互斥锁、自旋锁、读写锁……理清它们的区别和应用
二叉树基本知识
Pycharm cannot input Chinese solution
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]
Mutex lock, spin lock, read-write lock... Clarify their differences and applications
C # basic learning (XXIII)_ Forms and events
Docekr learning - MySQL 8 master-slave replication setup deployment
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
领域驱动模型设计与微服务架构落地-模型设计
Gym安装、调用以及注册