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

Python mock:模擬繼承的基類

Python mock: mocking base class for inheritance(Python mock:模擬繼承的基類)
本文介紹了Python mock:模擬繼承的基類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在測試一個從另一個非常復雜的類繼承的類,具有數據庫連接方法和一堆依賴項.我想模擬它的基類,以便我可以很好地使用子類中定義的方法,但是在我從模擬類繼承的那一刻,對象本身變成了模擬并丟失了它的所有方法.

I am testing a class that inherits from another one very complex, with DB connection methods and a mess of dependences. I would like to mock its base class so that I can nicely play with the method defined in the subclass, but in the moment I inherit from a mocked class, the object itself turns a mock and loses all its methods.

如何模擬超類?

這種情況或多或少可以概括為:

More or less the situation can be summed up in this:

import mock

ClassMock = mock.MagicMock()

class RealClass(ClassMock):

    def lol(self):
        print 'lol'

real = RealClass()
real.lol()  # Does not print lol, but returns another mock

print real # prints <MagicMock id='...'>

這是一個簡化的案例.實際發生的是 RealClass 擴展了 AnotherClass,但我設法攔截了 AnotherClass 并將其替換為模擬.

This is a simplified case. What is actually happening is that RealClass extends AnotherClass, but I managed to intercept the AnotherClass and replace it with a mock.

推薦答案

這應該適合你.

import mock

ClassMock = mock.MagicMock # <-- Note the removed brackets '()'

class RealClass(ClassMock):

    def lol(self):
        print 'lol'

real = RealClass()
real.lol()  # Does not print lol, but returns another mock

print real # prints <MagicMock id='...'>

您不應該像以前那樣傳遞類的實例.mock.MagicMock 是一個類,所以你直接傳遞它.

You should'nt pass an instance of the class as you did. mock.MagicMock is a class, so you pass it directly.

In [2]: inspect.isclass(mock.MagicMock)
Out[2]: True

這篇關于Python mock:模擬繼承的基類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 的意思不同)
主站蜘蛛池模板: 天天操综合 | 福利av在线 | 日韩一区二区在线视频 | 一级黄视频 | www.婷婷| 日韩欧美一区二区三区久久婷婷 | 亚洲欧美网站 | 一区二区三区高清 | 色天堂影院 | 日韩一区二区三区视频 | 国产精品一区二区视频 | 黄色成人在线 | 黄色小视频在线播放 | 欧美性生交xxxxx久久久 | 国产精品一区二区三区在线 | 欧美成人精品 | 日韩精品不卡 | 第一福利视频 | 香蕉视频免费看 | 天天插夜夜操 | 日韩黄色片| 一本一道久久a久久精品蜜桃 | 香蕉视频在线观看网站 | 一级黄色性生活片 | 久久久久人 | 天堂国产 | 91看片淫黄大片 | 欧美日本一区 | 亚洲免费观看 | 日韩小视频在线观看 | 一级黄色片免费 | 欧美不卡 | 99热99| 成人免费在线播放 | 特级毛片爽www免费版 | 久久久久久九九九九 | 天天天操| av高清在线 | 麻豆国产91在线播放 | 黄色一级免费 | xxxx色 |