久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查多個模擬的調用順序

Checking call order across multiple mocks(檢查多個模擬的調用順序)
本文介紹了檢查多個模擬的調用順序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試測試三個函數的調用順序.

I have three functions that I'm trying to test the call order of.

假設在模塊 module.py 我有以下內容

Let's say that in module module.py I have the following

# module.py    

def a(*args):
    # do the first thing

def b(*args):
    # do a second thing

def c(*args):
    # do a third thing


def main_routine():
    a_args = ('a')
    b_args = ('b')
    c_args = ('c')

    a(*a_args)
    b(*b_args)
    c(*c_args)

我想檢查 b 是否在 a 之后和 c 之前被調用.因此,為 a、b 和 c 中的每一個進行模擬很容易:

I want to check that b is called after a, and before c. So getting a mock for each of a, b and c is easy:

# tests.py

@mock.patch('module.a')
@mock.patch('module.b')
@mock.patch('module.c')
def test_main_routine(c_mock, b_mock, a_mock):
    # test all the things here

檢查每個單獨的模擬是否被調用也很容易.如何檢查通話相對于彼此的順序?

Checking that each of the individial mocks are called is easy, too. How do I check the order of the calls relative to one another?

call_args_list 不起作用,因為它是為每個模擬單獨維護的.

call_args_list won't work as it's maintained separately for each mock.

我嘗試使用副作用來記錄每個調用:

I've tried using a side effect to log each of the calls:

calls = []
def register_call(*args):
    calls.append(mock.call(*args))
    return mock.DEFAULT

a_mock.side_effect = register_call
b_mock.side_effect = register_call
c_mock.side_effect = register_call

但這只會給我調用模擬的參數,而不是調用所針對的實際模擬.我可以添加更多邏輯:

But this only gives me the args that the mocks were called with, but not the actual mock that the call was made against. I can add a bit more logic:

# tests.py
from functools import partial

def register_call(*args, **kwargs):
    calls.append(kwargs.pop('caller', None), mock.call(*args, **kwargs))
    return mock.DEFAULT

a_mock.side_effect = partial(register_call, caller='a')
b_mock.side_effect = partial(register_call, caller='b')
c_mock.side_effect = partial(register_call, caller='c')

這似乎可以完成工作......但是有更好的方法嗎?感覺 API 中應該已經有一些東西可以做到這一點,而我卻錯過了.

And that seems to get the job done... Is there a better way though? It feels like there should already be something in the API that can do this that I'm missing.

推薦答案

定義一個 Mock 管理器并通過 attach_mock().然后檢查 mock_calls:

Define a Mock manager and attach mocks to it via attach_mock(). Then check for the mock_calls:

@patch('module.a')
@patch('module.b')
@patch('module.c')
def test_main_routine(c, b, a):
    manager = Mock()
    manager.attach_mock(a, 'a')
    manager.attach_mock(b, 'b')
    manager.attach_mock(c, 'c')

    module.main_routine()

    expected_calls = [call.a('a'), call.b('b'), call.c('c')]
    assert manager.mock_calls == expected_calls

只是為了測試它是否有效,更改 main_routine() 函數中的函數調用順序,添加查看它是否拋出 AssertionError.

Just to test that it works, change the order of function calls in the main_routine() function add see that it throws AssertionError.

在 跟蹤調用順序和不那么冗長的調用斷言

希望對您有所幫助.

這篇關于檢查多個模擬的調用順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 成人a在线观看 | 欧美黄 片免费观看 | h片在线观看免费 | 亚洲欧美日韩中文字幕一区二区三区 | 二区三区视频 | 久久99久久98精品免观看软件 | 视频在线一区二区 | 国产日韩久久久久69影院 | 三级黄色片在线 | 中文一区 | 国产精品亚洲一区二区三区在线 | 免费视频一区 | 欧美激情在线观看一区二区三区 | h视频在线观看免费 | 精品国产乱码久久久久久88av | 国产精品一区二区三区久久久 | 手机看片169 | 亚洲成人综合在线 | 久久国产精品久久久久久 | 九九色综合 | 欧美极品在线观看 | 国产精品乱码一二三区的特点 | 日日摸夜夜爽人人添av | 亚洲精色 | 81精品国产乱码久久久久久 | 日本黄色免费大片 | av黄色在线观看 | 日韩视频一区在线观看 | 国产女人第一次做爰毛片 | 在线日韩视频 | 91精品国产91久久久久久丝袜 | 日韩一二三区视频 | 日韩av一区二区在线观看 | 亚洲视频 欧美视频 | 一级a性色生活片久久毛片波多野 | 一区二区三区在线播放 | 日韩av一区二区在线观看 | 国产精品一区二区久久 | 日韩中文字幕第一页 | av在线一区二区三区 | 黄网免费看 |