如何优雅地停止python unittest? [英] How to gracefully stop python unittest?

查看:59
本文介绍了如何优雅地停止python unittest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python unittest 框架来测试外部设备.在这个特殊的用例中,我实际上想让 python 做一个需要几天时间的耐力测试.(一次测试只需要几分钟,但会经常重复.)

I'm using the python unittest framework to test external equipment. In this particular use case I'd actually like to have python do an endurance test that will take a couple of days. (One test will only take a couple minutes but it will be repeated very often.)

在这种情况下,我希望能够优雅地停止测试,然后再继续或重新启动它.我可以忍受稍后重新启动它,但现在我无法优雅地停止.

In this scenario I'd like to be able to gracefully stop the testing and either resume or restart it later. I can live with restarting it later, but right now I have no means of a graceful stop.

通过优雅的停止,我设想如下:按下 Ctrl-C 后,最后一个测试完成并返回结果.

By graceful stop I envision something like: Upon pressing Ctrl-C, last test is finished and the results are returned.

是否有任何内置的方法来实现这一目标?如果没有,我如何实现它?

Is there any built in way to achieve this? If not, how I achieve it otherwise?

推荐答案

我通过仔细查看 unittest 文档找到了解决方案:通过调用:

I found the solution by taking a much closer look at the unittest documentation: By calling:

unittest.installHandler()

安装了 Ctrl-C 的信号处理程序,并且所有结果都执行了:

a signal handler for Ctrl-C is installed and all results that do:

unittest.registerResult(result)

将在 Ctrl-C 的情况下正常退出.这正是我所需要的.

will gracefully exit in case of a Ctrl-C. Which is exactly what I needed.

这篇关于如何优雅地停止python unittest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆