我正在使用标准库Sector
模块(因此我使用<%=popup.ClientID %>
运行我的测试。)
我已定义WinAppDeployCmd install -file “<path>” -ip <ip> -pin <pin>
在后台启动子流程(使用unittest
)和python -m unittest
来关闭其输入和输出流,然后在其进程上调用setUpModule
ID。
如果我让测试运行的话,一切正常,但是如果我使用subprocess.Popen
提前停止它,我会收到一堆警告并且我的终端会慢慢爬行:
tearDownModule
是否有某些方法可以拦截os.killpg
以便正确清理?是否有更好的方法来启动和停止测试模块的外部程序?
答案 0 :(得分:1)
根据您的测试的组织方式,您还可以抓住KeyboardInterrupt
并在tearDown
区块中调用except
方法:
import unittest
class MyTestCase(unittest.TestCase):
def test_one(self):
for i in range(1<<20):
if i % 271 == 0:
print i
@classmethod
def tearDownClass(cls):
print("\nteardown")
if __name__ == '__main__':
try:
unittest.main()
except KeyboardInterrupt:
MyTestCase.tearDownClass()
答案 1 :(得分:1)
我发现的解决方案是重写unittest.TextTestRunner
并在KeyboardInterrupt
方法中捕获run()
,然后运行模块拆解。
class CustomTestRunner(unittest.TextTestRunner):
def _makeResult(self):
self.my_result = self.resultclass(self.stream, self.descriptions, self.verbosity)
return self.my_result
def run(self, test):
try:
return super().run(test)
except KeyboardInterrupt:
self.stream.writeln("Caught KeyboardInterrupt.")
self.my_result.printErrors()
test._handleModuleTearDown(self.my_result) # do cleanup
return self.my_result
答案 2 :(得分:0)
为了它的价值,我尝试按照https://stackoverflow.com/a/4205386/1475412中的说明进行操作并找到解决方案。
我为SIGINT
注册了一个处理子进程的处理程序,然后调用sys.exit()
。我以为我能够在处理程序中重新加注KeyboardInterrupt
但这不起作用。