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


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


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


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 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


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 import Kitchen It is equivalent to importing, And then you do the assignment kitchen =,kitchen Like y, Like x.

If the src.bubbleteashop.Kitchen Conduct patch when , 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 , Of Kitchen Will be replaced with MagicMock, and Of Kitchen Unaffected , Not by mock


If we import Import ?


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

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


Patch scope

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


Context manager

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


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


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


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.

Random recommended