TestResult Listeners in Unittest python Selenium

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() :

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(test, err) :

addFailure method gets a call when a test case is failed, this method will receive the error as a parameter

addSuccess(test) :

addSuccess method gets a call whenever a test passes

addSkip(test, reason) :

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(test, err)

addExpectedFailure method will be executed when a test method fails and that method annotated with expectedFailure() decorator.

addUnexpectedSuccess(test) :

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

    def test_2(self):
    @unittest.skip("not developed yet")
    def test_3(self):

    def test_4(self):
        self.assertEqual("test_4", "4th test", "test 4 is not same as 4th test")

    def test_5(self):

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)


About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions