Use `__name__ == "__main__":` within a class that implements `unittest.TestCase`

Viewed 40

I have a unittest.TestCase with a very heavy setup class

I would like to run it using a mock if the code is launched as main, and run the full data check if it runs trough an import as following:

import unittest
from utilities import create_full_data,create_mock_data

if __name__ == "__main__":
    print(' I want to run this block if code starts from here')
    data_to_check=create_mock_data()
else:
    print(' I want to run this block if imported')
    data_to_check = create_full_data()


class Test_payer_seg(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.data_to_test = data_to_check

    def test_data_qaulity(self):
        self.assertTrue(1==1)

The problem seems to occur since under the hood nosetests detects is as python tests and runs it as an internal process, hence __name__ can't possibly become 'main'.

How can I create a flow that runs it as mock if it is launched a main?

Note that this issue may happen since I'm running it using pycharm

1 Answers

pycharm recognize it as unit test and automatically runs it using the installed unit test package. You can create a new Python configuration with the script full path and working directory. This will run your main section without using the unit tests package.

Related