基于Robot Framework的接口自动化测试

Robot Framework框架简介

  • Robot Framework框架是一个通用的验收测试和验收测试驱动开发的自动化测试框架(ATDD),使用的是关键字驱动的测试方法。它本身拥有强大的标准库,此外还可以根据项目需要,使用Python对其测试库和框架本身进行扩展和优化,能同时满足对接口、数据库、UI自动化和服务器端自动化的测试需求,编写测试用例的成本低,维护方便。

项目说明

  • 本框架使用的是接口设计的分层玩法,分层的原则是:接口数据和接口业务分离。具体为:项目目录结构分层,接口数据与接口用例分离,接口用例与接口业务分离,公共方法和公共配置也接口业务分离,在测试用例中可以传入不定参数,同时也可以定义用例的执行顺序。

技术栈

  • Robot Framework
  • RequestsLibrary
  • HttpLibrary.HTTP

框架目录结构及相关说明

1、框架目录结构图如下

2、目录结构说明

  • 公共配置 ===========> 配置文件,定义公共请求头、请求域名等
  • 基础模块 ===========> 封装请求方法,公共方法和工具方法模块
  • 功能组件 ===========> 封装接口业务模块,接口依赖模块,多个接口组合复杂业务场景等
  • 主干用例&项目用例 ===========> 存放接口测试用例

代码设计与功能说明

1、封装请求方法,如下


*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           HttpLibrary.HTTP
Resource          ../icmcenterApi/公共配置/公共配置_index.txt

*** Keywords ***
SendPost
    [Arguments]    ${root_url}    ${uri}    ${ParameterDict}    ${DataType}    ${header}
    [Documentation]    ${root_url}:接口的host;
    ...    ${uri}:接口uri;
    ...    ${dict}:接口传入参数,字典数据类型;
    ...    ${DataType}:传入参数类型,如data等;
    ...    ${header}:请求头,字典类型。
    ...
    ...    响应数据为json类型。
    ...
    ...    若为独立请求,可在请求结束后立即释放连接。
    ${RequestData}    Create Dictionary
    log    ${ParameterDict.keys()}
    : FOR    ${key}    IN    @{ParameterDict.keys()}
    \    set to dictionary    ${RequestData}    ${key}    ${ParameterDict['${key}']}
    log    ${RequestData}
    create session    api    ${root_url}
    ${response}    post request    api    ${uri}    ${DataType}=${RequestData}    headers=${header}    timeout=${timeout}
    #    将json的string类型的数据转成python的字典类型
    Comment    ${ResponseBody}    To Json    ${response.content}
    sleep    ${sleepTime}
    log    ${response.text}
    [Return]    ${response.text}

SendGet
    [Arguments]    ${root_url}    ${uri}    ${ParameterDict}    ${header}
    [Documentation]    响应数据为json类型
    ${RequestData}    Create Dictionary
    log    ${ParameterDict.keys()}
    : FOR    ${key}    IN    @{ParameterDict.keys()}
    \    set to dictionary    ${RequestData}    ${key}    ${ParameterDict['${key}']}
    log    ${RequestData}
    create session    api    ${root_url}
    ${response}    get request    api    ${uri}    params=${RequestData}    headers=${header}    timeout=${timeout}
    Comment    ${ResponseBody}    To Json    ${response.content}
    sleep    ${sleepTime}
    log    ${response.text}
    [Return]    ${response.text}

re_session
    [Arguments]    ${host}
    [Documentation]    创建会话
    create session    session    ${host}

re_post
    [Arguments]    ${uri}    ${ParameterDict}    ${DataType}    ${header}
    [Documentation]    不创建会话,仅发送post请求;
    ...    响应数据为json类型
    ${RequestData}    Create Dictionary
    log    ${ParameterDict.keys()}
    : FOR    ${key}    IN    @{ParameterDict.keys()}
    \    set to dictionary    ${RequestData}    ${key}    ${ParameterDict['${key}']}
    log    ${RequestData}
    ${response}    post request    session    ${uri}    ${DataType}=${RequestData}    headers=${header}    timeout=${timeout}
    Comment    ${ResponseBody}    To Json    ${response.content}
    sleep    ${sleepTime}
    log    ${response.text}
    [Return]    ${response}

re_get
    [Arguments]    ${uri}    ${ParameterDict}    ${header}
    [Documentation]    不创建会话,仅发送get请求;
    ...    响应数据为json类型
    ${RequestData}    Create Dictionary
    log    ${ParameterDict.keys()}
    : FOR    ${key}    IN    @{ParameterDict.keys()}
    \    set to dictionary    ${RequestData}    ${key}    ${ParameterDict['${key}']}
    log    ${RequestData}
    ${response}    get request    session    ${uri}    params=${RequestData}    headers=${header}    timeout=${timeout}
    Comment    ${ResponseBody}    To Json    ${response.content}
    sleep    ${sleepTime}
    log    ${response.text}
    [Return]    ${response}
  • post请求

  • get请求

  • 以不定参数的形式封装请求,后续调用时,可传入不同个数的参数,方便不同场景下测试用例的设计

2、封装接口业务模块

  • 根据业务应用场景,组合接口模块
  • 发起请求,获取响应,并进行响应断言

3、测试用例设计

  • 根据业务场景,设计测试用例
  • 调用组合的接口模块,并依据场景需要,传入不同的参数

项目实例演示

posted @ 2019-12-09 16:03  军子~  阅读(2079)  评论(2编辑  收藏  举报