【robotframework】RequestLibrary的PUT/DELETE样例

通过Sanic实现了一个mock服务:

from sanic.response import json
from sanic import Blueprint

bp = Blueprint('mock_blueprint', url_prefix='mock')


@bp.route('/form', methods=['POST', ])
async def form_post(request):
    return json({'status': 'successful', 'message': 'Welcome to use Lion Mock Service. You have sent a POST message',
                 'body': request.json})


@bp.route('/form', methods=['GET', ])
async def form_get(request):
    return json({'status': 'successful', 'message': 'Welcome to use Lion Mock Service. You have sent a GET message',
                 'query_string': request.query_string})


@bp.route('/form/<form_arg:string>', methods=['PUT', ])
async def form_put(request, form_arg):
    return json({'status': 'successful', 'message': 'Welcome to use Lion Mock Service. You have sent a PUT message',
                 'form_arg': form_arg, 'body': request.json})


@bp.route('/form/<form_arg:string>', methods=['DELETE', ])
async def form_delete(request, form_arg):
    return json({'status': 'successful', 'message': 'Welcome to use Lion Mock Service. You have sent a DELETE message',
                 'form_arg': form_arg})


@bp.route('/weather', methods=['GET', ])
async def lion_get(request):
    msg = {'date': '2021-02-07', 'weather': 'sunny'}
    return json(msg)

 

调用PUT/DELETE服务的RF测试用例如下:

*** Settings ***
Library    Collections
Library    RequestsLibrary

*** Variables ***
${MOCK_URL}    http://192.168.10.109:8000

*** Test Cases ***   
mock_form接口测试-PUT
    [Documentation]    通过PUT请求调用Mock服务的/mock/form/<put_arg>
    Create Session    mock    ${MOCK_URL}
    # 待发送消息体
    &{post_data}=    Create Dictionary    k1=v1    k2=v2
    # PUT的URL变量
    Set Test Variable    ${put_arg}    put_arg1 
    
    ${resp}=    Put On Session    mock    /mock/form/${put_arg}    json=${post_data}
    # 获取返回的form_arg
    ${form_arg}=    Get From Dictionary    ${resp.json()}    form_arg
    Should Be Equal    ${form_arg}    ${put_arg}
    # 获取返回的body
    ${body}=    Get From Dictionary    ${resp.json()}    body
    Dictionaries Should Be Equal    ${post_data}    ${body}     
     
mock_form接口测试-DELETE
    [Documentation]    通过DELETE请求调用Mock服务的/mock/form/<del_arg>
    Create Session    mock    ${MOCK_URL}
    # DELETE的URL变量
    Set Test Variable    ${del_arg}    del_arg1 
    
    ${resp}=    Delete On Session    mock    /mock/form/${del_arg}
    # 获取返回的form_arg
    ${form_arg}=    Get From Dictionary    ${resp.json()}    form_arg
    Should Be Equal    ${form_arg}    ${del_arg}

 

posted on 2021-02-20 16:35  东西南北风  阅读(421)  评论(0编辑  收藏  举报

导航