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

我可以在 python 單元測試中偽造/模擬我的模擬對

Can I fake/mock the type of my mock objects in python unittests(我可以在 python 單元測試中偽造/模擬我的模擬對象的類型嗎)
本文介紹了我可以在 python 單元測試中偽造/模擬我的模擬對象的類型嗎的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的 python 代碼中,我檢查其中一個參數的類型以確保它是我期望的類型.例如:

In my python code I check the type of one of the parameters to make sure it is of the type I expect. For instance:

def myfunction(dbConnection):
    if (type(dbConnection)<>bpgsql.Connection):
        r['error'] += ' invalid database connection'

我想傳遞一個模擬連接以進行測試.有沒有辦法讓模擬對象偽裝成正確的類型?

I want to pass a mock connection for testing purposes. Is there a way to make the mock object pretend to be of the correct type?

推薦答案

恕我直言,看來你們不太對勁!

With all due respect, It looks like you guys are not quite right!

如上所述,我可以使用鴨子打字,但有一種方法可以做我最初打算做的事情:

I can use duck typing as said, but there is a way to do what I intended to do in the first place:

來自 http://docs.python.org/dev/library/unittest.mock.html

使用類或實例作為 specspec_set 的模擬對象能夠通過 isintance 測試:

Mock objects that use a class or an instance as a spec or spec_set are able to pass isintance tests:

>>>
>>> mock = Mock(spec=SomeClass)
>>> isinstance(mock, SomeClass)
True
>>> mock = Mock(spec_set=SomeClass())
>>> isinstance(mock, SomeClass)
True

所以我的示例代碼如下:

so my example code would be like:

m = mock.MagicMock(spec=bpgsql.Connection)
isinstance(m, bpgsql.Connection) 

返回真

話雖如此,我并不是在爭論在 python 中進行嚴格的類型檢查,我說如果你需要檢查它你可以做到,它也適用于測試和模擬.

All that said, I am not arguing for strict type checking in python, I say if you need to check it you can do it and it works with testing and mocking too.

這篇關于我可以在 python 單元測試中偽造/模擬我的模擬對象的類型嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 国产精品一区二区在线播放 | 国产一区二区在线91 | 久久精品视频网站 | 香蕉久久网 | 午夜电影日韩 | 免费不卡视频 | 亚洲欧美综合精品久久成人 | 亚洲精品电影网在线观看 | 久久中文字幕一区 | 亚洲啊v | 精品国产一区二区在线 | 日本超碰| 日韩伦理电影免费在线观看 | 亚洲性在线 | 国产精品中文字幕在线 | 四虎午夜剧场 | 欧美无乱码久久久免费午夜一区 | 午夜小视频在线观看 | 91亚洲国产亚洲国产 | 国产在线精品一区二区 | 欧美一区二区 | 午夜免费av | 美女久久| 国产一区二区三区免费 | 欧美精品一二三 | 日韩在线不卡 | 亚洲成人三级 | 欧美专区在线 | 国产精品美女久久久 | 做a视频在线观看 | 草草视频在线免费观看 | 日本三级黄视频 | 久久99精品久久久久久 | 在线播放第一页 | www国产成人免费观看视频,深夜成人网 | 一级黄色影片在线观看 | 日韩亚洲视频 | 亚洲激情一区二区 | 欧美日韩在线一区 | 亚洲传媒在线 | 日韩精品一区二区三区 |