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

Python mock:模擬繼承的基類

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

問題描述

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

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

這是一個簡化的案例.實際發(fā)生的是 RealClass 擴展了 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)該像以前那樣傳遞類的實例.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

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

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
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 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 日韩视频免费看 | 国产乱码精品一区二区三区五月婷 | 秋霞a级毛片在线看 | www.久久久久久久久久久久 | 日日夜夜精品视频 | 久久免费观看一级毛片 | 超碰在线97国产 | 欧美久久久久久久久 | 国产欧美日韩综合精品一区二区 | 国产精彩视频一区 | 99精品一级欧美片免费播放 | 在线黄 | 国产乱码一区 | 午夜免费福利影院 | 国产精品自在线 | 久久一| 中文字幕高清一区 | 国产高清不卡 | 日韩中文字幕在线观看 | 日韩欧美中文 | 中文字幕视频在线 | 亚洲福利av | 亚洲欧美日韩精品久久亚洲区 | 国产综合久久久 | 精品国产乱码久久久久久图片 | 亚州毛片 | 美国黄色毛片 | 不卡一区二区在线观看 | 欧美成人精品激情在线观看 | 欧美日韩三级 | 91精品久久久久久久久中文字幕 | 久久亚洲一区二区三区四区 | 色婷婷综合久久久久中文一区二区 | www.成人.com | 国产精品久久久久aaaa | 中文字幕99 | av资源网站 | 国产资源在线播放 | 成年人在线观看视频 | 黄色亚洲网站 | 国产精品久久久久久久久久久久冷 |