久久久久久久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)
主站蜘蛛池模板: 亚洲综合色丁香婷婷六月图片 | 一区二区日韩 | 国产一区二区免费 | 涩涩导航| 日韩成人一区 | 久久99精品久久久久久国产越南 | 亚洲啊v在线 | 午夜精品一区 | 久久久久久久综合色一本 | 国产精品无码专区在线观看 | 久久99精品久久久97夜夜嗨 | 日本色综合 | 国产精品久久久久久妇女6080 | 日韩一级免费大片 | 精品视频一区二区三区在线观看 | wwww.xxxx免费 | 成人午夜视频在线观看 | 久久精品亚洲精品国产欧美 | 国产在线1区 | 国产精品一区在线观看 | 欧美成人高清 | 日本一区二区视频 | 国产黄色大片 | 国产精品久久久久久亚洲调教 | 国产男女视频网站 | 国产激情视频网站 | a级片在线观看 | 国产欧美日韩在线观看 | 国产成人99久久亚洲综合精品 | 99r在线| 国产精品久久毛片av大全日韩 | 久久一区二区三区四区 | 一区二区三区回区在观看免费视频 | 成人精品福利 | 欧美九九 | 国产成人亚洲精品 | 亚洲精品乱码久久久久久蜜桃91 | 中文字幕精品一区久久久久 | 一区二区三区国产视频 | 欧美综合视频在线 | 一区二区三区四区国产 |