1

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?

4
  • 1
    "These operations are executed correctly before any unit test in the package's modules run." - are you sure, because it seems like they aren't running. Commented Jun 19, 2015 at 15:55
  • @jonrsharpe I'm sure about that, I can print stuff inside the package setUp method to make sure it's running as expected. Commented Jun 19, 2015 at 15:59
  • Is __init__.setUp supposed to run when TestPackage is imported? I cannot duplicate that behavior. Commented Jun 19, 2015 at 16:36
  • @chepner I run the tests with nose, which allows for package level setUp and tearDown methods. The setUp method is running without doubt, before any other test case in the package, I can easily confirm that. Commented Jun 22, 2015 at 8:15

1 Answer 1

1

It looks like setUp() isn't being called, but if you are certain that it is, then it could be the way that you are importing TestPackage. Try importing like 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 to be called before you use the global. This same thing should apply to the second way you tried. But again, that is assuming that setUp is run. You can alias TestPackage if you feel it is necessary, or you should be able to import it if it is defined outside the method.

Since you are explicitly importing it, it is likely trying to make a copy of it, which isn't possible, since it is inside of the setUp() body.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.