我有以下配置类:
class ConfigB(object):
Id = None
def __Init__(self, Id):
self.Id = Id
在以下类中实例化并打印属性:
from config.ConfigB import ConfigB
class FileRunner(object):
def runProcess(self, Id)
cfgB = ConfigB(Id)
print(cfgB.Id)
我创建了以下测试类以对其进行测试,在此我试图模拟实例化和cfgB.Id属性调用:
import unittest
import unittest.mock imort MagicMock
import mock
from FileRunner import FileRunner
class TestFileRunner(unittest.TestCase):
@mock.patch('ConfigB.ConfigB.__init__')
@mock.patch('ConfigB.ConfigB.Id')
def test_methodscalled(self, cfgBId, cfgBInit):
fileRunner = FileRunner()
cfgBId.return_value = 17
cfgBInit.return_value = None
print(cfgBId)
fileRunner.runProcess(1)
请注意在调用fileRunner之前,print(cfgBId)语句。我得到以下输出:
<MagicMock name='Id' id='157297352'>
<MagicMock name='Id' id='157297352'>
由于某种原因,当我在测试类中设置返回值时:
cfgBId.return_value = 17
在FileRunner()类的行上不会被调用:
print(cfgB.Id)
我该怎么做才能正确显示我的配置属性?
还要注意,我的'ConfigB'类实例化要比上面显示的复杂得多,这就是为什么我要修补实例化和对Id属性的调用。
*更新:我已按照@mgilson的建议更改了班级,但仍无法正常工作:
import unittest
import unittest.mock imort MagicMock
import mock
from FileRunner import FileRunner
class TestFileRunner(unittest.TestCase):
@mock.patch('FileRunner.ConfigB')
def test_methodscalled(self, cfgB):
fileRunner = FileRunner()
cfgB.Id = 17
print(cfgBId)
fileRunner.runProcess(1)
我现在从两个打印语句中获得以下输出:
<MagicMock name='ConfigB' id='157793640'>
<MagicMock name='ConfigB().Id' id='157020512'>
有什么想法不能解决上述问题吗?
*解:
对@mgilson建议的测试方法进行较小的更改后,我就能使它工作:
import unittest
from unittest.mock import MagicMock
import mock
from FileRunner import FileRunner
class TestFileRunner(unittest.TestCase):
@mock.patch('FileRunner.ConfigB')
def test_methodscalled(self, configB):
fileRunner = FileRunner()
cfgB = MagicMock()
cfgB.Id = 17
#This will return the cfgB MagicMock when `ConfigB(Id)` is called in `FileRunner` class
configB.return_value = cfgB
print(cfgB.Id)
fileRunner.runProcess(1)
#To test whether `ConfigB(17)` was called
configB.assert_called_with(17)
我现在得到以下输出:
<MagicMock name='ConfigB' id='157147936'>
17
参考方案
在我看来,最好只替换FileRunner
名称空间中的整个ConfigB对象。然后您的测试如下所示:
import unittest
import unittest.mock imort MagicMock
import mock
from FileRunner import FileRunner
class TestFileRunner(unittest.TestCase):
@mock.patch('FileRunner.ConfigB')
def test_methodscalled(self, cfgB):
fileRunner = FileRunner()
cfgB.return_value.Id = 17
fileRunner.runProcess(1)
请注意,@mock.patch('FileRunner.ConfigB')
将用模拟替换ConfigB
命名空间中的FileRunner
类。然后,您可以配置模拟以执行您喜欢的任何操作-例如Id
等于17。
我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
如何在python中连接熊猫数据框 - python我有一个python对象列表,有些像这样team_1_players1=[] is a list 在team_1_players1 = []内部,存储了多个json对象。1st Json Object像这样[[{'age_days': '72', 'age_years': '30'…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
如何修复AttributeError:模块'numpy'没有属性'square' - pythonImprove this question 我已经将numpy更新为1.14.0。我使用Windows10。我尝试运行我的代码,但出现此错误: AttributeError:模块“ numpy”没有属性“ square”这是我的进口商品:%matplotlib inline import matplotlib.pyplot as plt import ten…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。