2

Almost certainly some easy points here for someone. I've been using Python for about a week and am getting a NameError: global name '_build_response' is not defined when I attempt to call a function to build a text fixture in my unit test. Been scratching my head for a while at this, code snippet looks like:

class HttpTestCase(unittest.TestCase):   

  def _build_response():
    #build and returns some text fixtures

  def test_http_get(self): 
    response = _build_response()

Am I missing something in my understanding of inheritance or scoping, or is there something more embarrassing? Any pointers appreciated.

markdsievers
  • 7,151
  • 11
  • 51
  • 83
  • 2
    BTW, if you want a setup function, call it `setUp` and store the objects as attributes of `self`. Then you don't have to call it at the top of every test. (Of course you'll still have to know why this doesn't work as soon as you run into it in other code.) –  May 01 '12 at 10:00
  • @delan Thanks. The example was a little contrived, other factors prevent this from being part of setUp - but its good advice. Cheers for your time. – markdsievers May 01 '12 at 20:55
  • This was the part of the [doco](http://docs.python.org/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls) I missed in order to understand this, [this](http://stackoverflow.com/questions/2709821/python-self-explained) question also helps. – markdsievers May 01 '12 at 23:34

1 Answers1

8
class HttpTestCase(unittest.TestCase):   

  def _build_response(self):           
      # build and returns some text fixtures
      pass

  def test_http_get(self): 
      response = self._build_response()

_build_response is a method of HttpTestCase class

San4ez
  • 8,091
  • 4
  • 41
  • 62