簡體   English   中英

與模塊python django mock同名的補丁函數

[英]patch function with same name as module python django mock

|-- my_module
|   |-- __init__.py
|   |-- function.py
`-- test.py

在function.py中:

import other_function

def function():
    doStuff()
    other_function()
    return

在__init__.py中

from .function import function

在我的test.py中

from django.test import TestCase
from mock import patch
from my_module import function

class Test(TestCase):

    @patch('my_module.function.other_function')
    def function_test(self, mock_other_function):
         function()

當我運行它時,我得到一個AttributeError:

<@task:項目的my_module.function.function:0x7fed6b4fc198>沒有屬性'other_function'

這意味着我正在嘗試修補函數“function”而不是模塊“function”。 我不知道如何理解我想修補模塊。

我還想避免重命名我的模塊或功能。

有任何想法嗎?

[編輯]您可以在https://github.com/vthorey/example_mock運行中找到一個示例

python manage.py test

您可以在__init__.py以不同的名稱使模塊可用:

from . import function as function_module
from .function import function

然后你可以在test.py執行以下操作:

from django.test import TestCase
from mock import patch
from my_module import function

class Test(TestCase):

    @patch('my_module.function_module.other_function')
    def function_test(self, mock_other_function):
         function()

我不認為這是一個特別優雅的解決方案 - 對於一個隨意的讀者來說代碼並不是很清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM