久久久久久久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 更改在另一個方法中調(diào)用的方法的返回值)
本文介紹了使用 python 的模擬 patch.object 更改在另一個方法中調(diào)用的方法的返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

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

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中任何時候調(diào)用它都會返回我在 @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.

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

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):
    ...

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

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

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

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

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調(diào)用)
Checking call order across multiple mocks(檢查多個模擬的調(diào)用順序)
主站蜘蛛池模板: 久久国产精品免费一区二区三区 | 罗宾被扒开腿做同人网站 | 亚洲国产精品人人爽夜夜爽 | 亚洲一区二区三区视频 | 成人黄色在线 | 亚洲国产一区二区三区 | 狠狠爱一区二区三区 | 成人高清视频在线观看 | 欧美国产日韩在线观看 | 久久影音先锋 | 久久网站黄 | 久操亚洲 | 在线一级片 | 99久久久无码国产精品 | 91精品国产91久久久久久吃药 | av在线天堂 | 久久精品视频网站 | 久久丝袜| 精品欧美一区二区中文字幕视频 | 久久精品中文字幕 | av一级毛片 | 一级毛片免费视频观看 | 免费超碰 | 久久久久无码国产精品一区 | 久久电影一区 | 亚洲黄色一级毛片 | 国产精品美女久久久久久不卡 | 欧美精产国品一二三区 | 精品一区二区在线观看 | 一区二区三区在线 | 日韩影院在线观看 | 青青草国产在线观看 | 欧美一区二区三区久久精品 | 九九视频在线观看视频6 | 欧美日韩一区二区三区四区 | 精品一区二区三 | 视频一区中文字幕 | 亚洲欧美日本在线 | 高清色视频 | 日韩在线中文 | 亚洲欧美日韩久久 |