taverntesting / tavern

A command-line tool and Python library and Pytest plugin for automated testing of RESTful APIs, with a simple, concise and flexible YAML-based syntax
https://taverntesting.github.io/
MIT License
1.02k stars 193 forks source link

How to pass parameters to function in verify_response_with #831

Closed SabreenKaur closed 1 year ago

SabreenKaur commented 1 year ago

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?

michaelboulton commented 1 year ago

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
SabreenKaur commented 1 year ago

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?

michaelboulton commented 1 year ago

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