vendredi 26 août 2016

Where to catch a KeyboardInterrupt in unittests?

I'm UI testing on mobile devices using an Appium server. I want to be able to cancel the testing process while developing the tests. Currently, when I CTRL-C out of the process, I have to restart the appium server, since the session was not shut down properly (this would have been done in the tearDown() method of the test, but since I press CTRL-C, that won't be executed.) Instead I wanna have the tearDown() fire everytime the test gets canceled by a KeyboardInterrupt.

Now's my question: Where do I put the try-catch block to achieve this? Is there a best practice handling this in Python unittests? I need to access a class variable (self.driver.quit()), right after KeyboardInterrupt fires. The class variable is inside the class that was put into the unittest.TestSuite.

try:
  self.test_something()
except KeyboadInterrupt:
  self.driver.quit()

I've looked a bit into unittest.TestResult and its stop() method but haven't found practical examples explaining its usage properly.

Aucun commentaire:

Enregistrer un commentaire