How To Force Tests To Stop Running A Test Suite After A Specified Test Failed?
I have a test suite written in Selenium Webdriver/Python 2.7 consisting of several test cases. Some of test cases are so critical that if they fail the whole test is failed and the
Solution 1:
What about something like:
import unittest
classCustomResult(unittest.TestResult):
defaddFailure(self, test, err):
critical = ['test4', 'test7']
if test._testMethodName in critical:
print("Critical Failure!")
self.stop()
unittest.TestResult.addFailure(self, test, err)
classTestSuite1(unittest.TestCase):
defsetUp(self):
passdeftest1(self):
returnTruedeftest2(self):
returnFalsedeftest3(self):
returnTrue# This is a critical testdeftest4(self):
self.fail()
passdeftest5(self):
print("test5")
returnTruedeftearDown(self):
passif __name__ == '__main__':
runner = unittest.runner.TextTestRunner(resultclass=CustomResult)
unittest.main(testRunner=runner)
You may have to adjust this depending on how you invoke your testing.
If self.fail()
(in test4
) is commented out, 5 methods are tested. But if it is not commented out, the test prints "Critical Failure!" and stops. In my case, only 4 tests ran.
It might also be smart to name those methods so that when sorted lexicographically, they come first, that way if a critical failure will happen, there is no time wasted testing the other methods.
Output (with self.fail()
):
Critical Failure! Ran 4 tests in 0.001s FAILED (failures=1)
Output (without self.fail()
):
test5 Ran 5 tests in 0.001s OK
Post a Comment for "How To Force Tests To Stop Running A Test Suite After A Specified Test Failed?"