• 【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}
  • 相关阅读:
    net5 webapi中 SwaggerUI如何进行版本控制
    动态菜单/权限管理的实现效果(数据前提:须做好 菜单、按钮、角色、用户等相关功能)
    MOS管的引脚,G、S、D分别代表什么?
    关系再好,也不要跟人透露这三个隐私。
    Linux 学习笔记
    算法 蓄水问题
    算法 等概率问题
    算法 字符串类问题(一)
    效率,生产力和用户友好的应用程序是GeneXus为Salinas集团带来的一些好处
    Multillantas Nieto通过智能设备和GeneXus将生产率提高了50%
  • 原文地址:https://www.cnblogs.com/frankzs/p/14421971.html
Copyright © 2020-2023  润新知