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

如何模擬一個裝飾函數

How to mock a decorated function(如何模擬一個裝飾函數)
本文介紹了如何模擬一個裝飾函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

出于測試原因,我需要能夠模擬在其他地方使用的裝飾的內部/原始功能:

For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else:

在 mydecorator.py 中:

In mydecorator.py:

def my_decorator(f):
    def wrapped_f():
        print "decorated"
        f()
    return wrapped_f


@my_decorator
def function_to_be_mocked():
    print 'original'


def function_to_be_mocked_undecorated():
    print 'original'


def run_decorated():
    function_to_be_mocked()


def run_undecorated():
    decorated_funtion = my_decorator(function_to_be_mocked_undecorated)
    decorated_funtion()

如您所見,我有幾個版本的原始函數 function_to_be_mocked,一個帶有裝飾器 my_decorator,另一個是裸".運行器函數 run_decorated() 調用 function_to_be_mocked 的修飾版本, run_undecorated() 調用未修飾版本并手動"應用修飾器.兩者的結果是一樣的:

As you can see, I have several versions of the original function function_to_be_mocked, one with the decorator my_decorator and one 'naked'. The runner function run_decorated() calls the decorated version of function_to_be_mocked and run_undecorated() calls the undecorated version and applies the decorator 'manually'. The result of both are the same:

decorated
original

現在我想測試 runner 函數,但我需要模擬原始函數 function_to_be_mocked 并且模擬的版本應該被裝飾:

Now I want to test the runner function but I need to mock the original function function_to_be_mocked but also the mocked version should be decorated:

import unittest
import mydecorator
from mock import patch

def mock_function():
    print 'mockified'

class Test(unittest.TestCase):

    @patch('mydecorator.function_to_be_mocked_undecorated')
    def test_undecorated_mocked(self, mock_function_to_be_mocked_undecorated):
        mydecorator.function_to_be_mocked_undecorated = mock_function
        mydecorator.run_undecorated()
        assert 1==0

    @patch('mydecorator.function_to_be_mocked')
    def test_decoratorated_mocked(self, mock_function_to_be_mocked):
        mydecorator.function_to_be_mocked = mock_function
        mydecorator.run_decorated()
        assert 1==0

這對于未修飾的版本 test_undecorated_mocked 可以正常工作:

This works as expected for the undecorated version test_undecorated_mocked:

decorated
mockified

但裝飾版給出:

mockified

所以裝飾器消失了.

是否可以讓裝飾版本以與未裝飾版本相同的方式工作,其中手動"應用裝飾器?

Is it possible to get the decorated version working in the same way as the undecorated version, where the decorator is applied 'manually'?

我嘗試在裝飾器中暴露內部函數,但沒有成功.

I tried to expose the inner function in the decorator without success.

我看到了這個問題 你如何模擬一個在單元測試中應用了裝飾器的函數?但這對我沒有幫助.

I saw this question How do you mock a function which has decorator apply to it in a unit test? but this doesn't help me.

推薦答案

Python在加載模塊時應用裝飾器,所以在test_decoratorated_mocked<中設置function_to_be_mockedmock_function/code> 確實會將該函數更改為未修飾的函數.

Python applies the decorator when loading the module so setting function_to_be_mocked to mock_function in test_decoratorated_mocked would indeed change that function into an undecorated function.

如果要模擬 function_to_be_mocked,則需要再次手動添加裝飾器:

You'd need to manually add the decorator again if you wish to mock function_to_be_mocked:

mydecorator.function_to_be_mocked = mydecorator.my_decorator(mock_function)

這篇關于如何模擬一個裝飾函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 久久久国产一区二区三区 | 午夜影院在线观看 | www.99热.com| 91人人在线 | 香蕉超碰 | a级片播放| 欧美久久久久久久久中文字幕 | 狠狠操狠狠干 | 黄色网址免费看 | 中文字幕在线视频免费视频 | 精品国产亚洲一区二区三区大结局 | 91社区在线观看 | 亚洲精色 | 欧美涩 | 成人在线视频观看 | 欧区一欧区二欧区三免费 | 国产亚洲一区二区三区 | av黄色在线| 欧美日韩三区 | 在线看成人av | 欧美视频免费在线 | 在线免费看黄 | 亚洲在线观看视频 | 国精产品一区二区三区 | www.亚洲一区 | 欧美日韩亚洲一区 | 天天躁天天操 | 特黄色一级毛片 | 天天天操操操 | 日韩一级不卡 | 亚洲二区视频 | 男女免费观看在线爽爽爽视频 | 超碰免费在线 | 国产 日韩 欧美 在线 | 久久精品99国产精品 | 精品国产一区久久 | 久久噜| 久久精品中文 | 欧美成人免费在线 | 91亚洲精品久久久电影 | 毛片入口|