When I create a test method in Django, what exactly does β€œI” mean?

My understanding of the self is that it means something like "an instance of a class that contains this method." So when the django test defaults to:

class SimpleTest(TestCase): def test_basic_addition(self): 

in a sense, we pass test_basic_addition an instance of SimpleTest to which at some point the actions defined in the setUp method (if any) were applied. Supposedly, manage.py test somehow creates this instance, but I really don't understand what it is! What's going on here?

+4
source share
1 answer

Indeed, the test runner creates an instance of your test class ( SimpleTest ) for each test method you create. self refers to this instance.

From the unittest documentation :

Each TestCase instance will run one test method: a method named methodName .

This means that you can use additional β€œhelper” methods in your test class and call them self.name_of_helper_method() .

Normally, you don’t have to worry about how the unittest structure loads and runs your tests, but the unittest documentation can explain this in more detail.

+3
source

Source: https://habr.com/ru/post/1443828/


All Articles