0

I have the following code for test class. Looks like teardown_class does not get invoked when exception is raised in setup_class. How can I make teardown_class run even if exception appears in setup_class?

import unittest


class LoginTests(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        print("setup_class")
        raise Exception("ex")


    def test_01_login_with_valid_password(self):
        pass

    @classmethod
    def teardown_class(cls):
        print("teardown_class")

if __name__ == "__main__":
    unittest.main()
4
  • You need to use the correct spellings of the setup and teardown. Also, why do you want the teardown to run when an exception occurred in the setup? Teardown is only sensible when the setup was successful. Commented Apr 18, 2022 at 10:33
  • @quamrana I am running selenium tests. In setup_class I am launching browser. I need to close it in teardown_class no matter what Commented Apr 18, 2022 at 10:47
  • 1
    Do the answers to this question help at all? Commented Apr 18, 2022 at 10:53
  • @quamrana yes it does, thanks for pointing that one for me Commented Apr 18, 2022 at 10:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.