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

Python mock:模擬繼承的基類

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

問(wèn)題描述

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

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='...'>

這是一個(gè)簡(jiǎn)化的案例.實(shí)際發(fā)生的是 RealClass 擴(kuò)展了 AnotherClass,但我設(shè)法攔截了 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.

推薦答案

這應(yīng)該適合你.

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='...'>

您不應(yīng)該像以前那樣傳遞類的實(shí)例.mock.MagicMock 是一個(gè)類,所以你直接傳遞它.

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

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

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 亚洲欧洲中文 | 久久免费观看一级毛片 | 欧美99| 亚洲精品黑人 | 女人天堂av | 久久久久久国产精品久久 | 成人免费精品视频 | 欧美福利一区 | 中文字幕在线观看第一页 | 91国在线高清视频 | 国产96色在线| 久久综合一区二区三区 | 国产精品成人国产乱一区 | 91在线精品秘密一区二区 | 久久国产精品72免费观看 | 在线一区视频 | www.久久久.com | 亚洲一级av毛片 | 麻豆hd| 伊人青青久久 | 欧美日韩国产精品 | 国产成人精品视频 | 国产精品高潮呻吟久久 | 亚洲精品资源 | 精品日韩一区二区三区av动图 | 91精品国产综合久久婷婷香蕉 | 国产不卡一区在线观看 | 亚洲精品乱码久久久久久按摩观 | 国产中文字幕在线 | 懂色中文一区二区三区在线视频 | 中文字幕第三页 | 精品视频999 | 日韩免费在线视频 | 国产精品视频 | 欧美久久久电影 | 欧美极品少妇xxxxⅹ免费视频 | www.日日干 | 91精品在线播放 | 欧美精品网站 | 欧美极品视频在线观看 | 国产美女特级嫩嫩嫩bbb片 |