传递——form文件作为ROBOT框架的一部分



我试图在ROBOT框架中传递以下API请求

curl --request POST --url <API End Point>  --form 'sourcefile=@/home/test.zip' --header "Authorization: <Bearer Token>"

等效机器人测试用例,

Sample Test Case
[Arguments]    ${token}=default
Create Session    mxesession    ${mxe_host}
${accessToken}=    Catenate    Bearer    ${token}
${data}=    Create Dictionary    sourcefile=/home/test.zip
${header}=    Create Dictionary    Authorization=${accessToken}
${response}=   Post Request    mxesession    /v1/ml   files=${data}    headers=${header}
Should Be Equal As Strings    ${response.status_code}    200
当我执行上面的测试用例时,它成功通过了。但是该文件不能被应用程序成功处理,而当它通过curl请求直接传递时,它被成功处理。所以,我想说的是,我在这里传递的文件没有问题,而通过curl请求命令和机器人框架测试用例传递给应用程序的文件似乎不同。

测试用例是否正确,特别是我传递' sourcefile '的方式

?我应该把">@/home/test.zip在robot框架中这个路径不同吗?

链接:通过REST API使用multipart/form-data上传文件,这是Bryan Oakley的建议。

正确的测试用例如下:

Sample Test Case
[Arguments]    ${token}=default
Create Session    mxesession    ${mxe_host}
${accessToken}=    Catenate    Bearer    ${token}
${fileData}=    Get Binary File  /home/test.zip
&{fileParts}=  Create Dictionary
Set To Dictionary  ${fileParts}  sourcefile=${fileData}
${response}=   Post Request    mxesession    /v1/ml   files=${fileParts}    headers=${header}
Should Be Equal As Strings    ${response.status_code}    200

关键行是:

${fileData}=    Get Binary File  /home/test.zip
&{fileParts}=  Create Dictionary
Set To Dictionary  ${fileParts}  sourcefile=${fileData}

最新更新