当前位置:网站首页>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 :

边栏推荐
- 电脑里一辈子都不想删的神仙软件
- Int array get duplicate data
- 【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
- Basic knowledge of binary tree
- IM系统-消息流化一些常见问题
- How to use handwritten JDBC?
- Django 2 ----- database and admin
- Hcip day 9 notes
- Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board
- 2022全球开发者中,你的收入排多少?
猜你喜欢

QGIS加载在线地图:高德、天地图等

Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework

Applet enterprise red envelope function
![Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component

Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE

VIM basic operation summary

JS array indexof includes sort() colon sort quick sort de duplication and random sample random

Discussion on principle and application technology of MLIR

uniapp处理后台传输图片

Install oh my Zsh
随机推荐
并发编程之阻塞队列
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
二叉树基本知识
From input URL to web page display
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
为提高效率使用ParallelStream竟出现各种问题
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
Azure Devops (XIV) use azure's private nuget warehouse
Common methods of arrays
Turn off automatic update when brew executes commands
C#基础学习(二十三)_窗体与事件
刷题-洛谷-P1089 津津的储蓄计划
Numpy简介和特点(一)
Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE
刷题-洛谷-P1047 校门外的树
window unbutu20 LTS apt,wget 安装时 DNS 解析错误
mujoco+spinningup进行强化学习训练快速入门
Talk about your understanding of hashcode and equals methods?
ES6数组去重 new Set()
[platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1