简体   繁体   中英

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.

What could be the best approach to stop executing the test_demo whenever there is an error in setup method.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM