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

檢查多個(gè)模擬的調(diào)用順序

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

問題描述

我正在嘗試測試三個(gè)函數(shù)的調(diào)用順序.

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

假設(shè)在模塊 module.py 我有以下內(nèi)容

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 之前被調(diào)用.因此,為 a、b 和 c 中的每一個(gè)進(jìn)行模擬很容易:

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

檢查每個(gè)單獨(dú)的模擬是否被調(diào)用也很容易.如何檢查通話相對于彼此的順序?

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 不起作用,因?yàn)樗菫槊總€(gè)模擬單獨(dú)維護(hù)的.

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

我嘗試使用副作用來記錄每個(gè)調(diào)用:

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

但這只會(huì)給我調(diào)用模擬的參數(shù),而不是調(diào)用所針對的實(shí)際模擬.我可以添加更多邏輯:

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 中應(yīng)該已經(jīng)有一些東西可以做到這一點(diǎn),而我卻錯(cuò)過了.

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.

推薦答案

定義一個(gè) 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() 函數(shù)中的函數(shù)調(diào)用順序,添加查看它是否拋出 AssertionError.

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

在 跟蹤調(diào)用順序和不那么冗長的調(diào)用斷言

希望對您有所幫助.

這篇關(guān)于檢查多個(gè)模擬的調(diào)用順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 182tv午夜 | 中文字幕视频 | a级黄毛片| 国产区在线视频 | 亚洲国产欧美日韩 | 免费三级网站 | 亚洲精品福利视频 | 日韩a在线| 色av导航| 亚洲综合激情网 | 日韩久久久 | 99国产免费| 欧美大片18| 日韩一区二区在线播放 | 久久福利网 | 一区二区av | 中文在线免费观看 | 精品一区二区在线播放 | 岛国精品在线播放 | 亚洲青涩 | 日韩精品视频在线免费观看 | www.日本高清| 亚洲一区二区欧美 | 在线观看黄色小说 | 2021国产精品| 国产亚洲视频在线观看 | 欧美顶级黄色大片免费 | 亚洲视频在线观看 | 欧美亚洲国产精品 | 国产免费黄色片 | 又色又爽又黄18网站 | 免费黄色片视频 | 国产一级大片 | 成人免费看片在线观看 | 天天射天天舔 | 日韩欧美中文 | 日韩香蕉视频 | 日本福利在线 | 欧美日韩综合网 | 亚洲黄色三级 | 99热播|