我很难理解 python 的模拟测试方法。
我想对这个文件做一些模拟。
由于包 xbmc、xbmcaddon 和 xbmcgui 无法在正常的 python 环境中导入,我设法像这样模拟它们:
class XBMCTestCase(unittest.TestCase):
def setUp(self):
#Mock up any calls to modules that cannot be imported
self.xbmc = Mock()
self.xbmcgui = Mock()
self.xbmcaddon = Mock()
modules = {
'xbmc' : self.xbmc,
'xbmcgui': self.xbmcgui,
'xbmcaddon': self.xbmcaddon
}
self.module_patcher = patch.dict('sys.modules', modules) #@UndefinedVariable
self.module_patcher.start()
在这里查看它的实际应用。
因此,当我导入 setlocation.py 时,会出现如下错误:
File "/home/paulo/workspace/weather.metoffice/src/metoffice/utils/utilities.py", line 21, in <module>
CACHE_FOLDER = os.path.join(ADDON_DATA_PATH, 'cache')
File "/usr/lib/python2.7/posixpath.py", line 78, in join
path += b
TypeError: unsupported operand type(s) for +=: 'Mock' and 'str'
即使我模拟了“metoffice.utils”(通过将其添加到安装时创建的补丁中的模块列表中),我也会遇到类似的错误setlocation.py
File "/home/paulo/workspace/weather.metoffice/src/metoffice/setlocation.py", line 32, in <module>
GEOIP_PROVIDER = int(__addon__.getSetting('GeoIPProvider'))
TypeError: int() argument must be a string or a number, not 'Mock'
所以我需要__addon__.getSetting()
返回一个字符串。
有任何想法吗?
所有尝试都失败了,但我认为我没有完全掌握模拟包的功能。
注意我在 Python 2.7.3 和mock 1.0.1