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

使用 python 的模擬 patch.object 更改在另一個方法中

Using python#39;s mock patch.object to change the return value of a method called within another method(使用 python 的模擬 patch.object 更改在另一個方法中調用的方法的返回值)
本文介紹了使用 python 的模擬 patch.object 更改在另一個方法中調用的方法的返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否可以模擬在我嘗試測試的另一個函數中調用的函數的返回值?我希望模擬方法(將在我正在測試的許多方法中調用)在每次調用時返回我指定的變量.例如:

Is it possible to mock a return value of a function called within another function I am trying to test? I would like the mocked method (which will be called in many methods I'm testing) to returned my specified variables each time it is called. For example:

class Foo:
    def method_1():
       results = uses_some_other_method()
    def method_n():
       results = uses_some_other_method()

在單元測試中,我想用mock來改變uses_some_other_method()的返回值,這樣在Foo中任何時候調用它都會返回我在 @patch.object(...)

In the unit test, I would like to use mock to change the return value of uses_some_other_method() so that any time it is called in Foo, it will return what I defined in @patch.object(...)

推薦答案

有兩種方法可以做到這一點;帶補丁和帶補丁.object

There are two ways you can do this; with patch and with patch.object

Patch 假定您不是直接導入對象,而是您正在測試的對象正在使用它,如下所示

Patch assumes that you are not directly importing the object but that it is being used by the object you are testing as in the following

#foo.py
def some_fn():
    return 'some_fn'

class Foo(object):
    def method_1(self):
        return some_fn()

#bar.py
import foo
class Bar(object):
    def method_2(self):
        tmp = foo.Foo()
        return tmp.method_1()

#test_case_1.py
import bar
from mock import patch

@patch('foo.some_fn')
def test_bar(mock_some_fn):
    mock_some_fn.return_value = 'test-val-1'
    tmp = bar.Bar()
    assert tmp.method_2() == 'test-val-1'
    mock_some_fn.return_value = 'test-val-2'
    assert tmp.method_2() == 'test-val-2'

如果是直接導入要測試的模塊,可以使用patch.object,如下:

If you are directly importing the module to be tested, you can use patch.object as follows:

#test_case_2.py
import foo
from mock import patch

@patch.object(foo, 'some_fn')
def test_foo(test_some_fn):
    test_some_fn.return_value = 'test-val-1'
    tmp = foo.Foo()
    assert tmp.method_1() == 'test-val-1'
    test_some_fn.return_value = 'test-val-2'
    assert tmp.method_1() == 'test-val-2'

在這兩種情況下 some_fn 都將在測試功能完成后取消模擬".

In both cases some_fn will be 'un-mocked' after the test function is complete.

為了模擬多個函數,只需在函數中添加更多裝飾器并添加參數以接收額外的參數

In order to mock multiple functions, just add more decorators to the function and add arguments to take in the extra parameters

@patch.object(foo, 'some_fn')
@patch.object(foo, 'other_fn')
def test_foo(test_other_fn, test_some_fn):
    ...

請注意,裝飾器越接近函數定義,它在參數列表中的位置就越早.

Note that the closer the decorator is to the function definition, the earlier it is in the parameter list.

這篇關于使用 python 的模擬 patch.object 更改在另一個方法中調用的方法的返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 国产精品一区二区精品 | 日韩国产在线 | 国产精品中文字幕在线播放 | 日韩福利在线 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 免费欧美 | 网站一区二区三区 | 精品国产一区二区三区av片 | 在线播放国产一区二区三区 | 国产亚洲一区二区精品 | 亚洲一区二区久久 | 久久骚| 成人午夜视频在线观看 | 91中文 | 亚洲精品电影在线 | 色精品 | 成人在线免费视频 | 99婷婷| 波多野结衣先锋影音 | 亚洲成人av一区二区 | 亚洲精品www | 国产免费一区二区三区 | 亚洲精品久久久久久一区二区 | 久久精品久久综合 | 欧美一级网站 | 久久一区二 | 免费亚洲婷婷 | 亚洲国产精品一区二区三区 | 亚洲中字在线 | 国产精品久久久久久中文字 | 狠狠操网站 | 欧美日韩淫片 | 观看av| 精品国产不卡一区二区三区 | 国产日韩欧美激情 | 91精品国产麻豆 | 荷兰欧美一级毛片 | 中文字幕欧美一区二区 | 韩国成人在线视频 | 欧美一级免费观看 | 99国产精品99久久久久久粉嫩 |