久久久久久久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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 围产精品久久久久久久 | 免费成人毛片 | 伊人久久综合 | 99久久精品一区二区成人 | 日韩国产中文字幕 | 91久久精品一区二区二区 | 亚洲精品中文在线观看 | 久久噜噜噜精品国产亚洲综合 | 成人做爰www免费看 午夜精品久久久久久久久久久久 | 免费看黄视频网站 | 午夜精品一区二区三区三上悠亚 | 欧美日韩国产三级 | 日韩二三区| 久久久久久综合 | 日韩高清在线观看 | 国产高清免费 | 狠狠色香婷婷久久亚洲精品 | 久久综合影院 | 中文字幕在线免费 | 亚洲a人| 三级免费av | 国产成人在线观看免费 | 涩爱av一区二区三区 | 欧美vide| 在线一区 | 超碰婷婷 | 欧州一区二区 | 中国一级特黄真人毛片 | 99热欧美 | 91视视频在线观看入口直接观看 | 日本在线免费看最新的电影 | 国产精品入口 | 亚洲国产成人精品女人久久久野战 | 羞羞视频免费在线观看 | 国产精品国产三级国产aⅴ浪潮 | 日韩精品一区二 | 国产精品99久久久久久www | 久久久久久久一级 | 男女午夜激情视频 | www.xxxx欧美 | 一区二区免费 |