当前位置:网站首页>Pytest.mark.parameterize and mock use
Pytest.mark.parameterize and mock use
2022-07-25 13:42:00 【Muxiatong】
Catalog
pytest.mark.parametrize
Now there is zjk.py modular , Want to test one of them add Function functions , Pass in x, y, hold x The result after square is similar to y Add and return .
zjk.py:
def power(x):
return x ** 2
def add(x ,y):
x = power(x)
return x + yCreate a test file , Test documents are generally in test Beginning or end , establish 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 == exceptsstay pycharm Test functions can be run separately in :

There is no error in the operation result , Explain that all test cases pass :

If the test case fails and does not meet the expected results , The error will prompt which use case fails :
@pytest.mark.parametrize
This is a function for parameter simulation settings , The test function in this example passed 4 Parameters , among x,y be necessary , The other two were added by myself , because x,y Is the incoming value to be measured , The other two are x The value after square , One is the expected output value , Corresponding relation , Here are two sets of test cases , And represents to run 2 Time :

call add Function to get the calculation result , keyword assert Assertion , The judgment result is different from the expected result , Don't wait for FAlse, Will not pass the use case , The screenshot above .
mock.patch
Look at the above. zjk.py Inside add The function calls a power Functional , and add Is the function you want to test , We just want to test the code of this function , The function procedure called halfway doesn't want to care , Want to use the value it returns directly , Because in case power Functions are network requests ,cmd Orders, etc , You need the Internet , Machines, etc , So in order to focus on testing , use patch simulation power Function return value
zjk.py Let's add another function :
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 modify :
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 == exceptsAdded @mock.patch, Represents the to be tested add The function called by the function intermediate procedure , Such as zjk.sub, Here, it means that what is passed in is sub Function path , Then you need to test the function test_add Passing in functions , The format is mock_ Function name , And there are sequence requirements , Order of incoming parameters ( Left to right ), Decorator sequence ( Down and up )
parametrize You also need to add power,sub Value of function , You can test
This is used here. if else To illustrate side_effect,return_value
return_value Good understanding is to set the return value , Such as mock_power.return_value = pow_res hold power The return value of the function , But it must be numeric or string
side_effect The requirement of assignment must be an iteratable object , So in the use case, there is one I took from the list
Set according to the actual situation , If you use it in reverse, you will report an error
Take a look at the results :

边栏推荐
- 刷题-洛谷-P1150 Peter的烟
- Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
- Gym安装、调用以及注册
- Applet H5 get mobile number scheme
- Audio and video technology development weekly | 255
- Blocking queue for concurrent programming
- How to realize the configuration method of user password free login?
- JS Array indexOf includes sort() 冒号排序 快速排序 去重和随机样本 random
- 刷题-洛谷-P1161 开灯
- Framework package merge script
猜你喜欢

面试官问我:Mysql的存储引擎你了解多少?

Excel record macro

ES6数组去重 new Set()

Audio and video technology development weekly | 255

arm架构移植alsa-lib和alsa-utils一路畅通

IM系统-消息流化一些常见问题

In order to improve efficiency, there are various problems when using parallelstream

刷题-洛谷-P1152 欢乐的跳

2022全球开发者中,你的收入排多少?

Uncaught SyntaxError: Octal literals are not allowed in strict mode.
随机推荐
QGIS加载在线地图:高德、天地图等
Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
2022年下半年软考初级程序员备考
Peripheral system calls SAP's webapi interface
Numpy快速入门
Excel record macro
0719RHCSA
2022年下半年软考信息安全工程师如何备考?
[configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
Hcip day 10 notes
From input URL to web page display
IM系统-消息流化一些常见问题
0710RHCSA
hcip第七天笔记
The migration of arm architecture to alsa lib and alsa utils is smooth
How to realize the configuration method of user password free login?
Basic knowledge of binary tree
C # basic learning (XXIII)_ Forms and events
Introduction to jupyter notebook
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?