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 192 forks source link

INQUIRY: upload multipart form file using external functions #898

Closed roaasallam closed 4 months ago

roaasallam commented 8 months ago

I have an endpoint which accepts multipart form data,

I have wrote python function to write random content in a csv file,

Is there a way to pass this file in the request data using the python function that returns the file?

thanks,

`

request:
  url: "{tavern.env_vars.TARGET_HOST}/file-upload"
  method: POST
  timeout: 10.0
  headers:
    Authorization: "Bearer {token}"
    Content-Type: "multipart/form-data; boundary=654249085280325934841628"
  files:
    file: "tests/acceptance/testdata/auto-written-file-upload.csv"
  data:
 # file: [ auto-written-file-upload.csv ] # what we do normally 
    file: [ testing_utils:read_from_file(filename=auto-written-file-upload.csv) ] # trying to get the file via python function 
response:
  status_code: [204] 

`

michaelboulton commented 7 months ago

If I understand the question correctly, I think this should work if you make your function return a dictionary with the file key like

def read_from_file(filename):
    return {
        "file": ...
    }

And use it in the test like

 files:
    file: "tests/acceptance/testdata/auto-written-file-upload.csv"
  data:
    $ext:
      function: testing_utils:read_from_file
      kwargs:
        filename: auto-written-file-upload.csv

Basically you can't have a $ext function anywhere except the top level of a 'data' json' etc. block