0

I was coding some python (specifically behave tests) and got burned by some sort of state memory.

In some other module, we have a method that builds http headers:

def create_login_headers(headers={}, username=DEFAULT, password=DEFAULT)

in my behave step file, I have a method that accepts a header argument, but calls the above if one is not supplied:

def post(header=create_login_headers())

Normally I would take the default header by just invoking post()

For some of my testing, I needed to use a non-default user. So I called post like this:

post(header=create_login_headers("unprivileged_person","password"))

and it worked fine.

but the next call to post, which was invoked due to a setup in the Background section, started failing!

It became apparent that the headers were the culprit. I added this work of art to my Background section:

@given(u'default headers')
def step_impl(context):
    h = create_login_headers()

and the issue went away. (h is not a global variable). Perhaps the {} in the create_login_headers is the culprit.

Finally, my question - what part of python cached the old headers? Does the function call in the post only do it once, and use the last value thereafter?

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
  • Never mind I see the brief mention of `python-behave` at the beginning now. Still, this question has little to nothing to do with that library. Let's keep our tags specific!~ – Two-Bit Alchemist Sep 02 '15 at 17:52
  • I had no idea, @Two-BitAlchemist. That's why I was here looking for help. – Tony Ennis Sep 02 '15 at 17:54
  • Understood. Your question reads to me like a general Python question so I just thought the tag was confusing. I saw on rereading why you put it there. No worries, it's fixed now. – Two-Bit Alchemist Sep 02 '15 at 17:56

0 Answers0