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?