Search code examples
pythonpython-3.xdjango-unittest

using self.method_name inside a class method


I'm new to OOP in Python. I'm using the unittest package for the first time. In the code below, which is from Python official documentation, when we use self.assertEqual are we calling the assertEqual method from the base class unittest.Testcase?

In general, whenever we call self.method_name inside another method definition of a class, does it call the method from the base class (assuming that method_name is not defined for the derived class)?

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = list(range(10))

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, list(range(10)))

Solution

  • As specified in the documentation for unittest, assertEqual is a method provided by the TestCase class.

    In general, when a method is accessed (via self.<method_name>) the base classes are then searched for that method using the MRO or method resolution order for that specific class.