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

如何模擬一個裝飾函數(shù)

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

問題描述

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

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()

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

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

現(xiàn)在我想測試 runner 函數(shù),但我需要模擬原始函數(shù) 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'?

我嘗試在裝飾器中暴露內(nèi)部函數(shù),但沒有成功.

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

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

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> 確實會將該函數(shù)更改為未修飾的函數(shù).

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)

這篇關(guān)于如何模擬一個裝飾函數(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 的意思不同)
主站蜘蛛池模板: 亚洲区一区二 | 日韩欧美三级 | 在线播放国产一区二区三区 | 在线国产欧美 | 一区二区三区四区在线视频 | 爱爱免费视频 | 国产精品中文字幕在线 | 国产福利在线 | 婷婷色婷婷| 中文字幕 亚洲一区 | 亚洲精品国产精品国自产在线 | 精品久久久久久亚洲精品 | 色男人的天堂 | 精品国产欧美日韩不卡在线观看 | 午夜影院黄| 久产久精国产品 | 亚洲成在线观看 | 国产精品一区2区 | h视频在线播放 | 免费一级欧美在线观看视频 | 丝袜一区二区三区 | 免费黄色网址视频 | 久久综合久久久 | 男人天堂免费在线 | 欧美久久久久 | 黄色一级免费观看 | 国产精品无码久久久久 | 伊人久久大香线 | 国产一区欧美一区 | 久久男人天堂 | 国产日产精品一区二区三区四区 | 国外成人在线视频 | 中文字幕成人av | 日韩精品在线观看视频 | 99亚洲精品视频 | www视频在线观看 | 亚洲综合精品 | 国内精品久久精品 | 亚洲精品自拍视频 | 亚洲精品视频免费 | 欧美性乱 |