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

如何使用 python 模擬庫模擬基類

How to mock a base class with python mock library(如何使用 python 模擬庫模擬基類)
本文介紹了如何使用 python 模擬庫模擬基類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試使用 mock 在 python 中編寫一些單元測試.

I try to use mock to write some unit-tests in python.

例如我有以下課程:

class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()

我只想測試 handle 方法.無需對 socketserver.BaseRequestHandler 做任何假設.例如,我想斷言 handle 使用參數 1024 調用 recv.可以用模擬做這樣的事情嗎?IE.用模擬替換基類 socketserver.BaseRequestHandler?還是我偏離了這個想法?

And I only want to test the handle method. Without having to assume anything about socketserver.BaseRequestHandler. I for example want to assert that handle calls recv with the argument 1024. Is it possible to do such thing with mock? I.e. replacing the base class socketserver.BaseRequestHandler with a mock? Or am I off track with that idea?

在 ecatmur 的回答下(謝謝!)我首先嘗試了以下方法:

With the answer of ecatmur (thank you!) I first tried the following:

patcher = patch.object(TCPHandler, '__bases__', (Mock,))
with patcher:
    patcher.is_local = True
    handler = TCPHandler()
    handler.handle()

但是現在 handle 不再被調用并且 dir(handler) 給出:

But now handle is not called anylonger and dir(handler) gives:

['assert_any_call', 'assert_called_once_with', 'assert_called_with', 'assert_has_calls', 'attach_mock', 'call_args', 'call_args_list', 'call_count', 'called', 'configure_mock', 'method_calls', 'mock_add_spec', 'mock_calls', 'reset_mock', 'return_value', 'side_effect']

type(handler) 給出<class 'mock.TCPHandler'>

我解釋說,修補基類也會將我的派生類變成一個模擬類.

Which I interpret that patching the base class also turns my derived class into a mock.

我現在嘗試了另一個想法:

I now gave another idea a try:

mock = MagicMock()
TCPHandler.handle(mock)
#assertions

但是模擬似乎沒有被調用.

However the mock seems not to be called.

推薦答案

您可以通過修補派生類的 __bases__ 來做到這一點:

You can do this by patching the derived class's __bases__:

def test_derived():
    patcher = mock.patch.object(Derived, '__bases__', (mock.Mock,))
    with patcher:
        patcher.is_local = True
        d = Derived()
        print d.foo()

is_local hack 是阻止 mock.patch 在反轉補丁時嘗試調用 delattr 所必需的.

The is_local hack is necessary to stop mock.patch from trying to call delattr when reversing the patch.

這篇關于如何使用 python 模擬庫模擬基類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 成人不卡视频 | 日韩欧美理论片 | 91成人午夜性a一级毛片 | 在线天堂免费中文字幕视频 | 久久久九九 | 日韩国产中文字幕 | 草草视频在线免费观看 | 日日草天天干 | 91久久精品一区二区二区 | 一级黄色片免费在线观看 | 欧美日韩国产综合在线 | 久久久久亚洲精品 | 欧美成人猛片aaaaaaa | 日韩在线不卡 | 米奇狠狠鲁| 国产婷婷综合 | 一级黄色毛片 | 亚洲一区中文字幕在线观看 | 日本大香伊一区二区三区 | 国产区免费视频 | 久草新在线 | 国产精品一卡二卡三卡 | 国产xxx在线观看 | 日韩欧美精品一区 | 日日摸天天添天天添破 | 91麻豆精品国产91久久久资源速度 | 亚洲精品黑人 | 国产一区二区 | 久久久成人一区二区免费影院 | 美女一级毛片 | 91av在线免费| 国产女人与拘做视频免费 | av中文字幕在线 | 久久丁香 | 中文av字幕 | 日本三级电影在线看 | 欧美在线视频观看 | 国产高清免费 | 欧美理伦片在线播放 | 综合色播 | 午夜影院黄 |