0

I'm trying to test an add function that excites in calc.py

import unittest
import calc
class TestCalc(unittest.TestCase):
    
    def test_add(self):
        result = calc.add(10,5)
        self.assertEqual(result,15)

My question is what does self means in this example

I know that self is an instance of a class.But, can you give me an example of what the value of self would be in my example.

1
  • Is this not covered in the unittest documentation? Commented Jun 26, 2020 at 1:34

1 Answer 1

1

self is a reference to the instance of TestCalc that is instantiated by the test runner from unittest when you run your tests.

More generally, when an object is instantiated and one of its methods is called (such as test_add(), called by unittest's test runner in your case), self is used to refer to the instance the method is called on, allowing access to its other properties and methods (such as assertEqual(), inherited from unittest.TestCase, in your case).

Sign up to request clarification or add additional context in comments.

2 Comments

You said (the instance the method is called on) It means that self refers to unites.TestCase in my example right? Thank you
No, it refers to an instance of your class TestCalc, which is a subclass of unittest.TestCase. I think you need object-oriented programming lessons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.