Test Listeners are nothing but the event listeners, listener methods will be invoked when there even happening around the tests. Events could be like failure, pass, skip and so on.
Let's learn the listeners present in the TestResult class in unittest python framework
startTestRun() gets a call once before any tests are executed.
stopTestRun gets a call once after all tests are executed. Above two methods will be useful only when you implement your own run method by extending the TestCase class or TestSuite class, otherwise below two methods will be invoked instead of the above two methods.
startTest(test) method runs after collecting the test methods and test suite information and runs before running any test method.
startTest method is applicable for each and every test method present in the test class, for example, if you have 10 test method then this method will be executed 10 times
stopTest(test) method gets a call after the test case has been executed, regardless of the outcome.
stopTest method is also applicable to each and every method in the test class.
addFailure method gets a call when a test case is failed, this method will receive the error as a parameter
addSuccess method gets a call whenever a test passes
addSkip method will be executed when a test gets skipped, all the skipped tests will have a reason why it is getting skipped (please refer skipTest section)
addExpectedFailure method will be executed when a test method fails and that method annotated with expectedFailure() decorator.
addUnexpectedSuccess method will be executed when a test method passed and that method annotated with expectedFailure() decorator.
import unittest from unittest.suite import TestSuite from unittest.case import expectedFailure class Test_Witch(unittest.TestCase): def test_1(self): self.fail('test1') #fails def test_2(self): print('test_2') #passes @unittest.skip("not developed yet") def test_3(self): print("test_3") #passes @expectedFailure def test_4(self): self.assertEqual("test_4", "4th test", "test 4 is not same as 4th test") #fails @expectedFailure def test_5(self): print("test_5") #passes class CustomResultListener(unittest.TestResult): def startTest(self, test): print('Starting test : ', test) def stopTest(self, test): print('Stopping test : ', test) def addFailure(self, test, err): print("Failed test : ", test, " with error ", err) def addSuccess(self, test): print("Test passed : ", test) def addSkip(self, test, reason): print("Test skipped : ", test, "with reason : ", reason) def addExpectedFailure(self, test, err): print("Test Expected Failure : ", test, "with reason ", err) def addUnexpectedSuccess(self, test): print("Test UnExpected Passed : ", test) if __name__ == "__main__": # load the test from test class tests = unittest.defaultTestLoader.loadTestsFromTestCase(Test_Witch) # create object for result class we created custom_result = CustomResultListener() # load tests in to suite suite = TestSuite(tests) # run the suite final_result = suite.run(custom_result)