current position:Home>Python testing - the patches in Python

Python testing - the patches in Python

2022-02-02 01:11:48 jingliang

What is? Patch

Mock It can be seen everywhere in unit testing . stay Python In unit testing , Common are unittest and pytest.unittest Corresponding Mock Kuo is unittest.mock,pytest The corresponding is pytest-mock. these two items. Mock All exist in the library Patchers,Patchers And Mock Interwovenness

Pytest-mock Essentially with unittest.mock It's the same API, So let's start with unittest.mock Speaking of

A simple example

1.png

When the test goal is BubbleTeaShop Of make_milk_tea Method , But I don't want to actually call Kitchen Of make_milk_tea When the method is used , You can use the @patch() Method pair class Conduct patch, And use instance.make_milk_tea.return_value Specifies the return value of the method

2.png

The above example bubbleteashop.py Medium Kitchen By @patch Replaced by ,@patch What is returned is a unittst.mock Medium MagicMock object

3.png

Where to Patch

Tell in official documents Patch Mentioned before using

Note: The key is to do the patching in the right namespace. See the section where to patch.

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined.

That is to say object Found ( By using ) The place where patch, Not defined places . And the document also mentions a premise :object It's through from import Imported

In the above example , We go through from src.kitchen import Kitchen How to import Kitchen,

therefore patch Yes. src.bubbleteashop.Kitchen, Instead of src.kictchen.Kitchen

The reasons are related to python The assignment mechanism is directly related to ( The picture is quoted from Ned Batchelder - Python Names and Values

4.png

Ned Batchelder stay Pycon 2015 When sharing in ,Python The variable in is the name of the reference value . If you assign a second name , These names do not refer to each other , They all refer to the same value (x And y All point to 23). If you then assign one of the names again (x=12), The other name is not affected (y Still 23)

In the previous example ,from src.kitchen import Kitchen It is equivalent to importing src.kitchen, And then you do the assignment kitchen = src.kitchen.Kitchen,kitchen Like y,src.kitchen.Kitchen Like x.

If the src.bubbleteashop.Kitchen Conduct patch when ,bubbleteashop.py Of Kitchen Will be replaced with MagicMock, This is exactly what you want to achieve in the test

If the src.kictchen.Kitchen Conduct patch when ,kitchen.py Of Kitchen Will be replaced with MagicMock, and bubbleteashop.py Of Kitchen Unaffected , Not by mock

5.png

If we import src.kitchen Import ?

6.png

import src.kitchen It's equivalent to being in bubbleteashop.py Chinese envoy src.kitchen Refers to the whole kitchen modular , visit src.kitchen.make_milk_tea Will enter the kitchen in , find make_milk_tea, And execute it .

If the src.kictchen.Kitchen Conduct patch when ,kitchen.py Of Kitchen Will be replaced with MagicMock, because bubbleteashop.py Of src.kitchen Refers to the whole kitchen modular , therefore mock It will also affect bubbleteashop.py

7.png

Patch scope

Patch There are three main ways , In every way scope Different , Different use scenarios

8.png

Context manager

Use with Make a statement patch,patch The scope of is limited to with The body of a statement

9.png

Function decorator

As in the above example, the created on the test method @patch(‘src.bubbleteashop.Kitchen’) It's just one. function decorator, Applies only to the current test method

10.png

Class decorator

Same use @pathch() Method to create , It's just created in class On , Act on this test class Every method of , It is applicable to multiple test methods sharing the same patcher

11.png

The Patchers in Python It's rich in content , You can refer to unittest and py-test Official documents of , Have the opportunity to continue sorting patchers More to use

copyright notice
author[jingliang],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020111458130.html

Random recommended