简体   繁体   中英

unittesting a function in different file in python

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM