[英]Unit test in Python, error in setup class
I am writing a test suite as: 我将测试套件编写为:
class MySuite(unnitest.Testcase):
@classmethod
def setUpclass(cls):
try:
***some code which will throw exception***
except Exception as e:
print('Error Thrown')
@classmethod
def tearDownClass(cls):
print('In teardown')
def test_demo(self):
print('In test_demo')
The problem is, though error will be thrown in setup ( because of 5/0 ), the test_demo will be executed, which I do not want to. 问题是,尽管将在安装过程中抛出错误( 由于5/0 ),但是test_demo将被执行,我不想这样做。
What could be the best approach to stop executing the test_demo whenever there is an error in setup method. 当安装方法中出现错误时,最好的方法是停止执行test_demo。
You're catching the error and just printing a message in response. 您正在捕获错误,只是打印一条消息作为响应。 Don't do that.
不要那样做
If you really want to print something, at least re-raise the exception afterwards. 如果您确实要打印某些内容,请至少在以后重新引发异常。 But better not to bother catching it at all: the traceback should be sufficient information.
但是最好不要打扰到它:回溯应该是足够的信息。
(Also, the first parameter to a classmethod is usually called cls
not self
, as it is the class itself.) (此外,类方法的第一个参数通常称为
cls
not self
,因为它是类本身。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.