How does a method of a class class run if you haven't instantiated an object for that class? Referring to Python's TestCase class
I am fairly new to Python (and programming in general) so this may be a noob question, but I was of the understanding that in python, when you create a class, and you create X methods within it, that if you want to leverage those methods, you need to instantiate the class or create an instance of the class.
I am reviewing the TestCase module (testing goat for TDD) and I notice that we can run the program (performs all the logic within the methods of the classes) without actually creating any instance objects to "call" those methods directly.
So my question is...how is it running.
Ex.
class AddStuff(TestCase):
def test_equal_two(self):
two = 1 + 1
self.assertEqual(two, 2)
When I run python manage.py test, this will run, even though I haven't created an instance of AddStuff...I don't get it...
You’re right that (in all reasonable circumstances) an object is required to call an ordinary method (cf. staticmethod
and classmethod
). When using this kind of unit-testing framework, the program (script) you run is not the file that contains this class. The program comes with the framework; it uses relatively advanced language features to discover classes like this that inherit from TestCase
, create instances of each, and call the test functions on those objects.