I am using the standard library unittest
module (so I run my tests with python -m unittest
).
I have defined setUpModule
to start a subprocess in the background (with subprocess.Popen
) and tearDownModule
to close its input and output streams, then call os.killpg
on its process ID.
This all works fine if I let a test run its course, but if I stop it early using Ctrl-C
, I get a bunch of warnings and my terminal slows to a crawl:
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'>
Is there some way I can intercept the KeyboardInterrupt
in order to clean up properly? Is there a better way to start and stop an external program for a test module?