1

I'm having trouble setting the value of a global variable in a function that I'm writing for unit tests.

The function is probably not ready to be used in a test. Or at least to be used to test in an easy manner, but I'm trying to work around that.

Here is an example of the function I'm trying to test:

def my_func_with_globals(filepath):
  spos=filepath.find(__my_global_var1)
  new_path = filepath[0:spos] + __my_global_var2
  return new_path

def some_function():
  ...
  my_func_with_globals(filepath)
  ...

if __name__ = '__main__':
  global __my_global_var1
  __my_global_var1='value1'  
  global __my_global_var2
  __my_global_var2='value2'
  ...
  some_function()

And here is an example of my test:

import unittest
from my_module import *

class UnitTestMyModule(unittest.TestCase):
  def test_my_func_with_globals(self):
    self.assertEqual(my_func_with_globals('arbitrary/file/path'), 'valid output')

Another example of my test using @kdopen's suggestion (gives me the same error):

import unittest
import my_module

class UnitTestMyModule(unittest.TestCase):
  def test_my_func_with_globals(self):
      my_module.__my_global_var1='some/value'
      my_module.__my_global_var2='second_val'
      self.assertEqual(my_module.my_func_with_globals('arbitrary/file/path'), 'valid output')

I keep getting the error:

NameError: global name '__my_global_var1' is not defined.

I've tried a few different things, but I can't get anything to work. Using unittest.mock.patch looks like it would work perfectly, but I'm stuck with what I currently have with v2.6.4.

1 Answer 1

1

The globals are defined with a double leading underscore, so they are not imported by the from my_module import * statement.

You can make them accessible with the following:

from my_module import __my_global_var1, __my_global_var2

Alternatively, if you used import my_module you can access them as my_module.__my_global_var1 etc.

But I don't see any reference to the global variables in your sample test case

Here's a simple example

a.py

__global1 = 1

def foo():
    return __global1

b.py:

import a

print "global1: %d" % a.__global1
print "foo: %d" % a.foo()
a.__global1 = 2
print "foo: %d" % a.foo()

And running b.py

$ python2.6 b.py 
global1: 1
foo: 1
foo: 2

UPDATE:

Dang it, missed the obvious

You declare the variables within the if test. That code doesn't run on import - only when you execute python my_module from the command line.

During importing, __name__ will be set to my_module, not __main__

So, yes - they are undefined when you call your unit test.

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

5 Comments

I've actually tried doing 'import my_module' and then setting the values to use in my test with my_module.__my_global_var1 and my_module.__my_global_var2, and I still get the same error 'NameError: global name '__my_global_var1' is not definted'
then it seems there's something else going on ... are the globals actually defined in my_module or does it import something else (or perhaps they're in the __init__.py)
have added working code, explicitly run using python v2.6 ... though it is v2.6.9
And I've just added the real problem with your code ;)
lol, good catch! I completely didn't realize that __name__ would be set to my_module, not __main__ on import. I set the global vars in the if test as my_module.__my_global_var1and the world is right again. Great explanation btw!

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.