使用 Python Mock 模拟函数 [英] Mocking Functions Using Python Mock

查看:30
本文介绍了使用 Python Mock 模拟函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 模拟模块.

我在模拟导入到模块中的函数时遇到了一些问题.

例如,在 util.py 我有

def get_content():返回东西"

我想模拟 util.get_content 以便它返回其他内容.

我正在尝试:

util.get_content=Mock(return_value="mocked stuff")

如果 get_content 在另一个模块内被调用,它实际上似乎从未返回模拟对象.我在如何使用 Mock 方面遗漏了什么吗?

请注意,如果我调用以下内容,则一切正常:

<预><代码>>>>util.get_content=Mock(return_value="模拟的东西")>>>util.get_content()嘲笑的东西"

但是,如果从另一个模块内部调用 get_content,它会调用原始函数而不是模拟版本:

<预><代码>>>>从 mymodule 导入 MyObj>>>util.get_content=Mock(return_value="模拟的东西")>>>m=MyObj()>>>m.func()东西"

mymodule.py

的内容

from util import get_contentMyObj 类:定义函数():获取内容()

所以我想我的问题是 - 如何从我调用的模块内部调用函数的 Mocked 版本?

看来 from 模块导入函数可能是这里的罪魁祸首,因为它没有指向 Mocked 函数.

解决方案

我想我有一个解决方法,虽然它仍然不太清楚如何解决一般情况

mymodule中,如果我替换

from util import get_contentMyObj 类:定义函数():获取内容()

导入工具MyObj 类:定义函数():util.get_content()

Mock 似乎被调用了.看起来命名空间需要匹配(这是有道理的).然而,奇怪的是我期望

import mymodulemymodule.get_content = mock.Mock(return_value="模拟的东西")

在原始情况下使用 from/import 语法(现在将 get_content 拉入 mymodule)中的技巧.但这仍然指的是未模拟的 get_content.

原来命名空间很重要 - 只需在编写代码时牢记这一点.

I am trying to Mock a function (that returns some external content) using the python mock module.

I'm having some trouble mocking functions that are imported into a module.

For example, in util.py I have

def get_content():
  return "stuff"

I want to mock util.get_content so that it returns something else.

I am trying this:

util.get_content=Mock(return_value="mocked stuff")

If get_content gets invoked inside another module, it never actually seems to return the mocked object. Am I missing something in terms of how to use Mock?

Note that if I invoke the following, things work correctly:

>>> util.get_content=Mock(return_value="mocked stuff")
>>> util.get_content()
"mocked stuff"

However, if get_content is called from inside another module, it invokes the original function instead of the mocked version:

>>> from mymodule import MyObj
>>> util.get_content=Mock(return_value="mocked stuff")
>>> m=MyObj()
>>> m.func()
"stuff"

Contents of mymodule.py

from util import get_content

class MyObj:    
    def func():
        get_content()

So I guess my question is - how do I get invoke the Mocked version of a function from inside a module that I call?

It appears that the from module import function may be to blame here, in that it doesn't point to the Mocked function.

解决方案

I think I have a workaround, though it's still not quite clear on how to solve the general case

In mymodule, if I replace

from util import get_content

class MyObj:    
    def func():
        get_content()

with

import util

class MyObj:    
    def func():
        util.get_content()

The Mock seems to get invoked. It looks like the namespaces need to match (which makes sense). However, the weird thing is that I would expect

import mymodule
mymodule.get_content = mock.Mock(return_value="mocked stuff")

to do the trick in the original case where I am using the from/import syntax (which now pulls in get_content into mymodule). But this still refers to the unmocked get_content.

Turns out the namespace matters - just need to keep that in mind when writing your code.

这篇关于使用 Python Mock 模拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆