我希望对以下内容进行单元测试:
import sys
if sys.platform == "darwin":
import OSX.Leap as Leap
elif 'linux' in sys.platform:
import Linux.Leap as Leap
如何在测试中确定是否已导入OSX.Leap或Linux.Leap?换句话说,我怎么知道我已经向模块添加了哪个版本的Leap类?
更新
如果使用,inspect
我可以得到:
<module 'Linux.params' from 'Linux/params.pyc'>
为了:
inspect.getmodule(Leap)
您可以使用__name__
属性访问模块的名称:
>>> import cStringIO as c
>>> c.__name__
'cStringIO'
>>> import StringIO as c
>>> c.__name__
'StringIO'
对于您而言:
if 'Linux' in module_to_test.Leap.__name__:
# Linux ...
另一种方法是使用__file__
属性。但这不适用于某些模块(例如C扩展模块)
根据OP的注释,UPDATELeap
不是模块,而是模块内部的类/类型。
您可以使用__module__
类的属性访问模块名称。
Leap.__module__
或者,就像您在更新的问题中所做的那样,使用inspect.getmodule
获取模块:
inspect.getmodule(Leap).__name__
inspect.getmodule(Leap).__file__
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句