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

基于輸入?yún)?shù)模擬python函數(shù)

Mocking python function based on input arguments(基于輸入?yún)?shù)模擬python函數(shù))
本文介紹了基于輸入?yún)?shù)模擬python函數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我們一直在使用 Mock for python.

We have been using Mock for python for a while.

現(xiàn)在,我們要模擬一個函數(shù)

Now, we have a situation in which we want to mock a function

def foo(self, my_param):
    #do something here, assign something to my_result
    return my_result

通常,模擬它的方法是(假設(shè) foo 是對象的一部分)

Normally, the way to mock this would be (assuming foo being part of an object)

self.foo = MagicMock(return_value="mocked!")

即使我調(diào)用 foo() 幾次我也可以使用

Even, if i call foo() a couple of times i can use

self.foo = MagicMock(side_effect=["mocked once", "mocked twice!"])

現(xiàn)在,我面臨一種情況,當(dāng)輸入?yún)?shù)具有特定值時,我想返回一個固定值.因此,如果假設(shè)my_param"等于something",那么我想返回my_cool_mock"

Now, I am facing a situation in which I want to return a fixed value when the input parameter has a particular value. So if let's say "my_param" is equal to "something" then I want to return "my_cool_mock"

這似乎在 mockito for python

when(dummy).foo("something").thenReturn("my_cool_mock")

我一直在尋找如何通過 Mock 實現(xiàn)同樣的目標,但沒有成功?

I have been searching on how to achieve the same with Mock with no success?

有什么想法嗎?

推薦答案

如果 side_effect_func 是一個函數(shù),那么該函數(shù)返回的是什么叫模擬返回.side_effect_func 函數(shù)被調(diào)用與模擬相同的論點.這允許您改變回報根據(jù)輸入動態(tài)調(diào)用的值:

If side_effect_func is a function then whatever that function returns is what calls to the mock return. The side_effect_func function is called with the same arguments as the mock. This allows you to vary the return value of the call dynamically, based on the input:

>>> def side_effect_func(value):
...     return value + 1
...
>>> m = MagicMock(side_effect=side_effect_func)
>>> m(1)
2
>>> m(2)
3
>>> m.mock_calls
[call(1), call(2)]

http://www.voidspace.org.uk/python/模擬/mock.html#calling

這篇關(guān)于基于輸入?yún)?shù)模擬python函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 九九综合九九 | 久久精品影视 | 97伊人 | 久久亚洲欧美日韩精品专区 | 国产成人免费观看 | 国产精品欧美一区二区三区不卡 | 亚洲精品视频在线看 | 亚洲欧洲在线观看视频 | 国产成人一区二区三区精 | 精品一区二区三区在线观看 | 午夜影院在线观看视频 | 最新中文在线视频 | 色偷偷噜噜噜亚洲男人 | 国产精品欧美精品日韩精品 | 午夜精品一区二区三区在线观看 | 91久色 | 自拍偷拍欧美 | 一区二区三区小视频 | 天天影视亚洲综合网 | 欧美日韩精品专区 | 日日天天| 麻豆久久久9性大片 | 先锋资源站 | 欧美成人a | 91免费在线播放 | 国产一区二区免费在线 | 一区二区三区四区在线视频 | 亚洲免费视频一区 | 日韩在线不卡 | 亚洲视频 欧美视频 | 一区二区三区精品在线 | 久久精品视频99 | 久草精品在线 | 久久99精品久久久久久 | 91视频在线看 | 高清视频一区二区三区 | 青娱乐自拍 | 日韩精品一区二区三区高清免费 | 日韩精品 | 精品一区二区三区日本 | 久久精品黄色 |