scope - Python: Unable to import global variable initialized in setUp method from init file -


i have package testing modules , inside init file have setup method operations. these operations executed correctly before unit test in package's modules run. inside setup method i'd initialize global variable , access other modules of package. 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 testpackage import spec_project import unittest  class testrules(unittest.testcase):     def setup(self):         spec_project.get_new_device() 

like

importerror: cannot import name spec_project

if initialize spec_project variable outside of setup method in init file can have access content not changed after operations in 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 testpackage import spec_project import unittest  class testrules(unittest.testcase):     def setup(self):         spec_project.get_new_device() 

like an

attributeerror: 'nonetype' object has no attribute 'get_new_device'

how can initialize spec_project variable inside setup method of init file , still have access other module in package?

it looks setup() isn't being called, if is, way importing testpackage. try importing this:

#testpackage/test_module.py import testpackage import unittest  class testrules(unittest.testcase):     def setup(self):         testpackage.spec_project.get_new_device() 

the setup() method has called before use global. same thing should apply second way tried. again, assuming setup run. can alias testpackage if feel necessary, or should able import if defined outside method.

since explicitly importing it, trying make copy of it, isn't possible, since inside of setup() body.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -