[英]How do I stop testing in Python unittest tearDown()?
I've got a large number of tests written using Python unittest
(Python 2.7.8) as a large TestSuite
. 我有大量使用Python unittest
(Python 2.7.8)作为大型TestSuite
编写的测试。 Many of these tests invoke other programs. 这些测试中有许多调用其他程序。 Sometimes these other programs dump core. 有时,这些其他程序会转储核心。 When they do, I want to discover that and ensure the test fails. 当他们这样做时,我想发现这一点并确保测试失败。 If some number of cores are dumped, I want to abort the entire test environment and exit rather than continuing: my total test suite has >6000 tests and if everything is dumping core it's useless (and dangerous: disk space etc.) to continue. 如果转储了一些内核,我想中止整个测试环境并退出而不是继续:我的整个测试套件具有> 6000个测试,并且如果所有内容都在转储内核,则继续使用是毫无用处的(并且很危险:磁盘空间等)。
In order to ensure we look for coredumps after every test (so I have the best possible idea of what program/invocation dumped core) I decided to look for cores in tearDown()
, which I am doing successfully. 为了确保我们在每次测试后都查找coredumps(因此,我对哪个程序/调用转储了内核有最好的了解),我决定在tearDown()
查找cores,这是我成功完成的。 If I find a core, I can run an assert variant in tearDown()
to specify that the test failed. 如果找到内核,则可以在tearDown()
运行一个assert变量,以指定测试失败。
But I can't figure out how to just give up on all my testing completely from within tearDown()
if I find too many cores. 但是,如果我发现太多内核,我无法弄清楚如何仅仅从tearDown()
完全放弃所有测试。 I even tried to run sys.exit("too many cores")
, but unittest case.py
catches every exception thrown by tearDown()
except KeyboardInterrupt
(if I try to raise that by hand my script hangs until I do a real ^C
). 我什至试图运行sys.exit("too many cores")
,但是unittest case.py
捕获了除KeyboardInterrupt
之外tearDown()
抛出的所有异常(如果我尝试手动将其挂起,直到我执行真正的^C
为止) )。
I thought about trying to call stop()
, but this is a method on the result and I can't find any way to get access to the result object from within tearDown()
(!). 我考虑过尝试调用stop()
,但这是结果的一种方法,我找不到任何方法可以从tearDown()
(!)中访问结果对象。
So far my only option seems to be to invoke os._exit()
which is really annoying because it keeps any results from being reported at all! 到目前为止,我唯一的选择似乎是调用os._exit()
,这确实很烦人,因为它根本阻止了报告任何结果!
Is there really no facility in Python unittest.TestCase
to tell the test environment to just stop right now, generate what results you have but don't run anything else? Python unittest.TestCase
真的没有工具告诉测试环境立即停止,生成您有什么结果,但是什么也不要运行吗?
Can you check how many cores have been dumped in setUp()
? 您可以检查setUp()
转储了多少个内核吗? If so, you could just call self.skipTest('Too many cores dumped.')
when things get bad. 如果是这样,当情况变糟时,您可以仅调用self.skipTest('Too many cores dumped.')
。
If you don't want to look for cores in setUp()
, you could probably use a class variable to hold the core dump count and check that instead. 如果您不想在setUp()
寻找核心,则可以使用类变量来保存核心转储计数并进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.