我有一个导入pythoncom (并从中使用pythoncom.CoCreateInstance )的python程序。我希望在不导入pythoncom的情况下为程序逻辑创建一个unittest (这样我也可以在Linux上运行测试)。
有哪些选项?我可以在不修改被测系统的情况下做到这一点吗?
到目前为止,我发现:
sys.modules["pythoncom"] = "test"
import module_that_imports_pythoncom我的问题是,如果我有:
from pythoncom.something import something我会得到:
ImportError: No module named something.something而且sys.modules["something.something"]或sys.modules["pythoncom.something.something"]也不能工作。
有什么想法吗?
发布于 2010-06-15 21:41:18
您可以将import pythoncom放入try except块中。
发布于 2011-11-27 02:13:33
如果您需要运行测试,并且这些测试实际上依赖于操作系统,那么您可能希望使用这些装饰器,例如:
def run_only(func, predicate):
if predicate():
return func
else:
def f(*args, **kwargs): pass
return f
def run_only_for_linux(func):
pred = lambda: sys.platform == 'linux2'
return run_only(func, pred)
@run_only_for_linux
def hello_linux():
"""docstring"""
print("hello linux")通过这种方式,您可以声明测试只在linux上运行,而不会在测试本身中增加丑陋的复杂性。
发布于 2010-06-16 17:02:07
好的,如果您在测试中修改了PYTHONPATH,并在名为pythoncom的测试目录中的文件系统上创建了一个包含所需子目录的新包,会怎么样呢?
https://stackoverflow.com/questions/3045657
复制相似问题