title: Python unitest - Use variables defined in module and class level setup functions, in tests
body: <p>I am doing Python unit testing using <em>nosetests</em> to experiment with <a href="http://docs.python.org/2/library/unittest.html#class-and-module-fixtures" rel="nofollow noreferrer">Python class and module fixtures</a>, to have minimal setup across my tests.</p>
  <p>The <strong>problem</strong> is I am not sure how to use any variables defined in the <code>setupUpModule</code> and the <code>setUpClass</code> functions in my tests (example: <code>test_1</code>).</p>
  <p>This is what I am using to try out:</p>
  <pre><code>import unittest
  
  def setUpModule():
      a = &quot;Setup Module variable&quot;
      print &quot;Setup Module&quot;
  
  def tearDownModule():
      print &quot;Closing Module&quot;
  
  class TrialTest(unittest.TestCase):
      @classmethod
      def setUpClass(cls):
          print a #&lt;======
          b = &quot;Setup Class variable&quot;
  
      @classmethod
      def tearDownClass(cls):
          print &quot;Closing Setup Class&quot;
  
      def test_1(self):
          print &quot;in test 1&quot;
          print a #&lt;======
          print b #&lt;======
  
      def test_2(self):
          print &quot;in test 2&quot;
  
      def test_3(self):
          print &quot;in test 3&quot;
  
      def test_4(self):
          print &quot;in test 4&quot;
  
      def test_5(self):
          print &quot;in test 5&quot;
  
  if __name__ == &quot;__main__&quot;:
      unittest.main()
  </code></pre>
  <p>The error I get is:</p>
  <pre><code>Setup Module
  ERROR
  Closing Module
  
  ======================================================================
  ERROR: test suite for &lt;class 'one_setup.TrialTest'&gt;
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File &quot;/Library/Python/2.7/site-packages/nose/suite.py&quot;, line 208, in run
      self.setUp()
    File &quot;/Library/Python/2.7/site-packages/nose/suite.py&quot;, line 291, in setUp
      self.setupContext(ancestor)
    File &quot;/Library/Python/2.7/site-packages/nose/suite.py&quot;, line 314, in setupContext
      try_run(context, names)
    File &quot;/Library/Python/2.7/site-packages/nose/util.py&quot;, line 469, in try_run
      return func()
    File &quot;/Users/patila14/Desktop/experimental short scripts/one_setup.py&quot;, line 13, in setUpClass
      print a
  NameError: global name 'a' is not defined
  
  ----------------------------------------------------------------------
  </code></pre>
  <p>Of course, if I do <code>gloabl a</code> and <code>global b</code>, it will work. Is there a better way?</p>