title: Unit test - "NameError: global name is not defined"
body: <p>I am trying to figure out how to write unit tests for my functions in Python - here's the code written below:</p>
  
  <pre><code>def num_buses(n):
      import math
      """ (int) -&gt; int
  
      Precondition: n &gt;= 0
  
      Return the minimum number of buses required to transport n people.
      Each bus can hold 50 people.
  
      &gt;&gt;&gt; num_buses(75)
      2
      """
      bus = int()
      if(n&gt;=0):
          bus = int(math.ceil(n/50.0))
      return bus
  </code></pre>
  
  <p>I am attempting to write test code but my results fail - here's the code I have started with:</p>
  
  <pre><code>import a1
  import unittest
  
  
  class TestNumBuses(unittest.TestCase):
      """ Test class for function a1.num_buses. """
      def test_numbuses_1(self):
          actual = num_buses(75)
          expected = 2
          self.assertEqual(actual, expected)
  
      # Add your test methods for a1.num_buses here.
  
  
  if __name__ == '__main__':
      unittest.main(exit=False)
  </code></pre>
  
  <p>When I run the module by pressing F5 - this is what I get -</p>
  
  <pre><code>E
  ======================================================================
  ERROR: test_numbuses_1 (__main__.TestNumBuses)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "C:\1-blog-cacher\TestNumBuses.py", line 8, in test_numbuses_1
      actual = num_buses(75)
  NameError: global name 'num_buses' is not defined
  
  ----------------------------------------------------------------------
  Ran 1 test in 0.050s
  
  FAILED (errors=1)
  </code></pre>
  
  <p>It should pass since the number of passengers are 75 and each bus can hold a maximum of 50 people - anything more than that will result in a rounding up of the figures.</p>