使用Robot Framework测试CRUD Web服务

时间:2015-01-10 14:42:22

标签: json rest http robotframework

使用机器人框架使用 JSON 媒体类型测试CRUD(或面向资源) Web服务的最简单方法是什么?

示例阅读互动:

GET /user/666 HTTP/1.1
Host: example.com


HTTP/1.1 404 Not Found

示例创建互动:

POST /user HTTP/1.1
Host: example.com
Content-Type: application/json

{
    "firstname":"Webber",
    "lastname":"Jim"
}


HTTP/1.1 201 OK
Content-Type: application/json

{
    "id": 9780596805821,
    "firstname":"Webber",
    "lastname":"Jim"
}

1 个答案:

答案 0 :(得分:1)

可以像这样使用standard librariesrobotframework-requests

*** Settings ***
Library        Collections
Library        OperatingSystem
Library        RequestsLibrary

*** Test Cases ****
Create User
         Create Session      example    http://example.com
  ${file_data}= 
    ...  Get Binary File     ${RESOURCES}${/}normal_user.json
  ${headers}=
    ...  Create Dictionary   Content-Type    application/json
  ${resp}=
    ...  Post Request        example    /user    data=${file_data}    headers=${headers}
         Should Be Equal As Strings
           ...               ${resp.status_code}  201
         Dictionary Should Contain Key
           ...               ${resp.json()}       id