taverntesting/tavern

How to pass parameters to function in verify_response_with

SabreenKaur opened this issue · 3 comments

I have a test which is using an external function I wrote in a separate python file called test_utils.py. I would like to be able to pass other parameters to this function so that I can use it in the function body. For example I would like to pass a file directory as a parameter to the function my-function in the below test

test_verify_results.tavern.yaml

  - name: verify result
    request:
      method: POST
      url: 'http://test:8080
      headers:
        Content-type: application/json
    response:
      status_code: 200
      verify_response_with:
        - function: test_utils:my-function

test_utils.yaml

def my_function(response, file_dir):
    <code using file_dir>

Is possible to pass parameters to own defined functions in this way?

You can pass them using the extra_kwargs key: https://tavern.readthedocs.io/en/latest/basics.html#calling-external-functions , eg

    response:
      status_code: 200
      verify_response_with:
        - function: test_utils:my-function
          extra_kwargs:
            file_dir: /path/to/dir

Thank you, I managed to use extra_args instead of extra_kwargs which worked for my purpose. Could I please ask what is the difference between the two and in what cases is it better to use one over the other?

There isn't any particular difference between them, it's just the difference between args and kwargs in python (kwargs are passed by name)