I have a package with testing modules and inside the init file I have a setUp method with some operations. These operations are executed correctly before any unit test in the package's modules run. Inside the setUp method I'd like to initialize a global variable and then access it from other modules of the package. But this doesn't work.
# TestPackage/__init__.py
def setUp():
global spec_project
core_manager = get_core_manager()
spec_project = core_manager.get_spec()
#TestPackage/test_module.py
from TestPackage import spec_project
import unittest
class TestRules(unittest.TestCase):
def setUp(self):
spec_project.get_new_device()
Like this I get an
ImportError: cannot import name spec_project
If I initialize the spec_project variable outside of the setUp method in the init file I can have access to it but its content is not changed after the operations in the setUp method.
# TestPackage/__init__.py
spec_project = None
def setUp():
global spec_project
core_manager = get_core_manager()
spec_project = core_manager.get_spec()
#TestPackage/test_module.py
from TestPackage import spec_project
import unittest
class TestRules(unittest.TestCase):
def setUp(self):
spec_project.get_new_device()
Like this I get an
AttributeError: 'NoneType' object has no attribute 'get_new_device'
How can initialize the spec_project variable inside the setUp method of the init file and still have access to it from other module in the package?
__init__.setUpsupposed to run whenTestPackageis imported? I cannot duplicate that behavior.