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

python mock - 在不妨礙實現的情況下修補方法

python mock - patching a method without obstructing implementation(python mock - 在不妨礙實現的情況下修補方法)
本文介紹了python mock - 在不妨礙實現的情況下修補方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有一種干凈的方法來修補對象,以便在測試用例中獲得 assert_call* 幫助程序,而無需實際刪除操作?

Is there a clean way to patch an object so that you get the assert_call* helpers in your test case, without actually removing the action?

例如,如何修改 @patch 行以使以下測試通過:

For example, how can I modify the @patch line to get the following test passing:

from unittest import TestCase
from mock import patch


class Potato(object):
    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2


class PotatoTest(TestCase):

    @patch.object(Potato, 'foo')
    def test_something(self, mock):
        spud = Potato()
        forty_two = spud.foo(n=40)
        mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)

我可能可以使用 side_effect 來破解它,但我希望有一種更好的方法可以在所有函數、類方法、靜態方法、未綁定方法等上以相同的方式工作.

I could probably hack this together using side_effect, but I was hoping there would be a nicer way which works the same way on all of functions, classmethods, staticmethods, unbound methods, etc.

推薦答案

與你的解決方案類似,但使用 wraps:

Similar solution with yours, but using wraps:

def test_something(self):
    spud = Potato()
    with patch.object(Potato, 'foo', wraps=spud.foo) as mock:
        forty_two = spud.foo(n=40)
        mock.assert_called_once_with(n=40)
    self.assertEqual(forty_two, 42)

根據文檔:

wraps:要包裝的模擬對象的項目.如果 wraps 不是 None 那么調用 Mock 會將調用傳遞給被包裝的對象(返回真實結果).模擬上的屬性訪問將返回一個 Mock 對象,包裝了被包裹的對應屬性對象(因此嘗試訪問不存在的屬性將引發 AttributeError).

wraps: Item for the mock object to wrap. If wraps is not None then calling the Mock will pass the call through to the wrapped object (returning the real result). Attribute access on the mock will return a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn’t exist will raise an AttributeError).

<小時>

class Potato(object):

    def spam(self, n):
        return self.foo(n=n)

    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2


class PotatoTest(TestCase):

    def test_something(self):
        spud = Potato()
        with patch.object(Potato, 'foo', wraps=spud.foo) as mock:
            forty_two = spud.spam(n=40)
            mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)

這篇關于python mock - 在不妨礙實現的情況下修補方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 成人亚洲视频 | 久草视频免费在线 | 国内精品偷拍 | 亚洲国产第一页 | 久久成人免费视频 | 中文字幕自拍 | 国产精品一品二区三区的使用体验 | 麻豆一级片 | 午夜影视 | 欧美日韩一区二区在线观看 | 中文字幕免费在线观看 | 久久er99热精品一区二区 | 国产性色av | 日韩欧美精品一区二区 | 日本精品网站 | 国产欧美日韩综合精品 | 免费成人毛片 | 亚洲一区二区三区四区在线 | 国产一级免费视频 | 免费av网址在线观看 | 婷婷午夜天 | 九九精品免费视频 | 国产精品久久一区二区三区 | 黄色大片av | 日韩久久久久久 | 免费看成人片 | a一级黄色片 | 国产成人精品一区二区三区在线观看 | 成人精品在线视频 | 国产精品一区二区在线免费观看 | 日本免费毛片 | av香蕉 | 日本不卡高字幕在线2019 | 丰满少妇av| 国产精品手机在线观看 | 欧美午夜视频 | 日本黄色a级片 | 精品欧美在线 | 欧美黑粗大 | 日韩av免费在线观看 | 成人免费视频观看视频 |