久久久久久久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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 日韩三级在线 | 久久精品91 | 精品欧美一区二区三区久久久 | 黄网站免费观看 | 狠狠久久综合 | 国产视频亚洲视频 | 国产欧美在线播放 | 天天躁日日躁aaaa视频 | 中国xxxx性xxxx产国 | 成人在线免费视频 | 视频三区 | 国产激情自拍视频 | 国产精品久久久久久久久 | 国产视频一视频二 | 91在线免费观看 | 久久久久国产一区二区三区 | 欧美日韩国产精品一区二区 | 国内在线视频 | 久久激情视频 | 日韩精品一区二区三区中文字幕 | 欧美精品二区三区 | 中国一级特黄真人毛片 | 国产aaaaav久久久一区二区 | 日韩欧美在线一区 | 欧美一级一| 欧美日韩综合 | 国产精品国产a | 男人天堂国产 | 久久草视频 | 日韩成人av在线 | 一区二区日韩 | 国产精品96久久久久久 | 久久久久国产精品免费免费搜索 | 黄色大片在线视频 | 日本久久久久久 | 97国产精品| 97起碰| 日本精品视频 | 午夜性视频 | 午夜在线精品 | 爱操av|