我使用的是标准库unittest
模块(因此我用python -m unittest
运行我的测试)。在
我定义了setUpModule
在后台启动一个子进程(使用subprocess.Popen
)和{os.killpg
如果我让一个测试运行它的进程,这一切都可以正常工作,但是如果我使用Ctrl-C
提前停止它,我会收到一堆警告,并且我的终端会慢到爬行状态:
keyboardInterrupt
sys:1: ResourceWarning: unclosed file <_io.FileIO name=6 mode='rb'>
/.../lib/python3.4/importlib/_bootstrap.py:2150: ImportWarning: sys.meta_path is empty
sys:1: ResourceWarning: unclosed file <_io.FileIO name=7 mode='wb'>
sys:1: ResourceWarning: unclosed file <_io.BufferedWriter name='/dev/null'>
有没有什么方法可以拦截KeyboardInterrupt
以便正确清理?有没有更好的方法来启动和停止测试模块的外部程序?在
根据测试的组织方式,您还可以捕获
KeyboardInterrupt
并调用except
块中的tearDown
方法:不管怎样,我试着按照https://stackoverflow.com/a/4205386/1475412中的说明找到了一个解决方案。在
我为
SIGINT
注册了一个处理程序,它杀死了子进程,然后调用了sys.exit()
。我以为我可以在处理程序中重新引发KeyboardInterrupt
,但那没用。在相关问题 更多 >
编程相关推荐