当前位置:网站首页>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 赋值的要求必须是可迭代对象,所以在用例中有一条我取了列表
根据实际情况进行设置,用反了会报错
来看一下结果:

边栏推荐
- Hcip day 6 notes
- 全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
- mujoco_py中文文档
- QGIS loading online map: Gaode, Tiandi map, etc
- 我的创作纪念日
- 互斥锁、自旋锁、读写锁……理清它们的区别和应用
- Hcip day 8 notes
- Uncaught SyntaxError: Octal literals are not allowed in strict mode.
- 0719RHCSA
- 【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
猜你喜欢

从输入网址到网页显示

【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)

Uniapp handles background transfer pictures

JS Array indexOf includes sort() 冒号排序 快速排序 去重和随机样本 random

Design and principle of thread pool

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

ES6数组去重 new Set()

VIM tip: always show line numbers

0710RHCSA

JS array indexof includes sort() colon sort quick sort de duplication and random sample random
随机推荐
Shell common script: judge whether the file of the remote host exists
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
二叉树基本知识
Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]
详解浮点数的精度问题
HTTP cache tongtianpian, there may be something you want
0715RHCSA
ES6 array de duplication new set()
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Excel record macro
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
Programmer growth chapter 27: how to evaluate requirements priorities?
Immortal software in the computer that I don't want to delete all my life
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
Numpy快速入门
0713RHCSA
2022年下半年软考初级程序员备考
录制和剪辑视频,如何解决占用空间过大的问题?