简体   繁体   English

Python模拟无法获得正确的return_value

[英]Python mocking can't get right return_value

I have the following function with image a python object from a module and having the method shape. 我有以下功能,从模块中获取一个python对象的图像,并具有方法形状。

def foo(image):
    print image.shape

I want to mock the object image and it's return value 我想模拟对象图像及其返回值

image = Mock()
image.shape.return_value = (0,0)
foo(image)
>>> <Mock name='mock.shape' id='4416382544'>

Calling foo(image), image.shape will return . 调用foo(image),image.shape将返回。 How can I make it return (0,0)? 如何使它返回(0,0)? (I need to get the value to test some condition like if image.shape = (0,0). I'm struggling to have it work. (我需要获取该值来测试诸如image.shape =(0,0)之类的某些条件。我正在努力使其正常工作。

Thanks for your help 谢谢你的帮助

image.shape = (0, 0)

Set the attribute directly. 直接设置属性。 return_value has nothing to do with attribute accesses. return_value与属性访问无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Python 中获取 DFS 的全局变量以返回正确的值 - Can't get global variable of DFS in Python to return the right value Python模拟 - return_value - 获得“真实”的返回值 - Python Mock - return_value - getting the “real” return value 有没有办法将存储过程 Return_Value 返回给执行它的 python 脚本? - Is there a way to return a Stored Procedure Return_Value to the python script executing it? 有人可以从 python 字节码解释 CALL_FUNCTION 和 RETURN_VALUE - Can someone explain CALL_FUNCTION and RETURN_VALUE from python bytecode python模拟方法返回模拟而不是return_value吗? - python mock on method returning mock rather than return_value? Python模拟获取地址而不是return_value - Python Mock getting address instead return_value Python修补类-方法return_value返回MagicMock - Python patched Class - method return_value returns MagicMock 难以理解 Python 的 return_value 在以下示例中模拟 Class - Hard to understand return_value for Python Mock Class in the following example 为什么 Python 认为 return_value 是一个字符串? - Why does Python believe return_value is a string? Python模拟:带有patch和return_value的意外结果 - Python Mock: Unexpected result with patch and return_value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM