见下面的代码,特别是数据部分。机器人不喜欢这个。
*** Test Cases ***
Add Patient
Log Add Patient
[Tags] Add_Patient
${number}= Generate Random Number ${4}
${data}= {"patients": [ new "sponsor": "test1", "protocol": "Blue18-B18VP1","site_number": "1001","integration_id": int_id_${number},"subject_number": "RT0001","subject_status": "T","randomization_date": "01Jan2017", "treatment_id": "B18VP2"} ]}
Create Session sw3 ${ENVIRONMENT_TO_RUN_AGAINST} debug=3
${resp}= Post Request sw3 ${ENVIRONMENT_TO_RUN_AGAINST}/api/v1/test1/patients
\ ... Content-Type:application/json
\ ... Authorization:authkey02 Accept=application/json
\ ... data=${data}
给出如下错误:
Creating keyword failed: No keyword with name '{"patients": [ new "sponsor": "test1", "protocol": "Blue18-B18VP1","site_number": "1001","integration_id": int_id_${number},"subject_number": "RT0001","subject_status": "T","randomization_date": "01Jan2017", "treatment_id": "B18VP2"} ]}' found.
答案 0 :(得分:0)
将数据作为动态值而非字符串传递非常重要。因此,不是创建字符串创建列表和/或字典 - 如果然后将其传递,它将表示为JSON。例如:
short
答案 1 :(得分:0)
您必须使用Set variable或Catenate之类的关键字来创建字符串,这是该错误消息试图告诉您的内容。当您想要跨多行定义字符串时,Catenate非常有用。
例如:
${data}= Catenate
... {"patients":
... [ new "sponsor": "test1",
... "protocol": "Blue18-B18VP1",
... "site_number": "1001",
... "integration_id": int_id_${number},
... "subject_number": "RT0001",
... "subject_status": "T",
... "randomization_date": "01Jan2017",
... "treatment_id": "B18VP2"
... ]
... }
但是,您问题中的数据似乎是无效的JSON。我不知道你试图用[ new "sponsor": "test1" ...
做什么。无论如何,要解决创建包含json数据的变量的问题,您需要在变量名后面的第一个单元格中使用关键字。
您必须将标题作为字典传递,而不是作为单独的参数传递。您可以使用Create Dictionary
创建一个字典,如下所示:
&{headers}= Create Dictionary
... Content-Type=application/json
... Authorization=authkey02
... Accept=application/json
要发布帖子请求,你应该使用命名参数,传递标题和数据,如下所示:
${resp}= Post Request sw3 ${ENVIRONMENT_TO_RUN_AGAINST}/api/v1/test1/patients
... headers=${headers}
... data=${data}
有关更多示例,请查看请求库本身的测试用例:
https://github.com/bulkan/robotframework-requests/blob/master/tests/testcase.txt