Python | Патчинг объектов модульного теста | Комплект-1

Опубликовано: 12 Апреля, 2022

Проблема заключается в написании модульных тестов и необходимости применять исправления к выбранным объектам, чтобы делать утверждения о том, как они использовались в тесте (например, утверждения о вызове с определенными параметрами, доступе к выбранным атрибутам и т. Д.).

To do so, the unittest.mock.patch() function can be used to help with this problem. It’s a little unusual, but patch() can be used as a decorator, a context manager, or stand-alone.

Code #1: Using unittest.mock.patch as a decorator
from unittest.mock import patch
import example
@patch("example.func")
  
def test1(x, mock_func):
    # Uses patched example.func
    example.func(x) 
    mock_func.assert_called_with(x)

 
Code #2: Using unittest.mock.patch as a decorator

with patch("example.func") as mock_func:
    example.func(x) 
    mock_func.assert_called_with(x)

 
Code #3: Using unittest.mock.patch to patch things manually.

p = patch("example.func")
mock_func = p.start()
example.func(x)
mock_func.assert_called_with(x)
p.stop()

 
Code #4: Stacking decorators and context managers to patch multiple objects

@patch("example.func1")
@patch("example.func2")
@patch("example.func3")
  
def test1(mock1, mock2, mock3):
    ...
def test2():
    with patch("example.patch1") as mock1,
    patch("example.patch2") as mock2,
    patch("example.patch3") as mock3:
    ...

patch() works by taking an existing object with the fully qualified name that you provide and replacing it with a new value. The original value is then restored after the completion of the decorated function or context manager. By default, values are replaced with MagicMock instances.
 
Code #5 : Example

x = 42
with patch("__main__.x"):
    print(x)
  
print (x)

Выход :

<MagicMock name = 'x' id = '4314230032'>
42

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.