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.

3
  • 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.) Commented May 1, 2012 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. Commented May 1, 2012 at 20:55
  • This was the part of the doco I missed in order to understand this, this question also helps. Commented May 1, 2012 at 23:34

1 Answer 1

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

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.