'Python - Why do I have to use if __name__ == '__main__' in my test file

I came across the unittest module and I'm a bit confused. I have a question on the following code in my file test_number.py:

import unittest
from module import number

class NamesTestCase(unittest.TestCase):
    def test_number(self):
        verify = number(4)
        self.assertEqual(verify, True)
        
if __name__ == '__main__':
    unittest.main()

(function number from module.py just verifies some properities of given number)

I want to ask about the if __name__ == '__main__' part. I know this ensures that the testing will not take place if I import this module, but why do I have to add this if statement in my test file if I'm running this directly? Or, in other words, why do I have to add this line of code if I know that, when I'm going to use the number() function somewhere in the future, I won't be importing it from test_number.py as test_number() but rather directly from module?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source