原文链接
优势
- 支持不同开发语言平台,java/python
- 可是用于测试api,同时也可以用于测试ui界面
- 使用伪代码进行编写case,简单易懂,学习成本低
- 可以在伪代码中执行Python代码
- 可是使用python定义伪代码关键字
- 接口丰富,可自定义程度高
劣势
- 有一定的学习成本
安装
安装
robot framework
pip install robotframework
安装http请求扩展包
robotframework-requests
pip install robotframework-requests
定义公有关键字
http请求中,会有一部分公有的内容,比如 header
- 在
config
文件夹中新建variables.robot
设置常用的参数
*** Settings ***Documentation variables *** Variables ***${Host} http://www.domian.com/ # 服务器主机${User-Agent} Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0 # 浏览器代理${Accept} text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8${Content-Type} application/x-www-form-urlencoded${Content-Type-Json} application/json${Accept-Language} en-US,en;q=0.5${Accept-Encoding} gzip, deflate${Cookie} 9${Connection} keep-alive${Cache-Control} max-age=0${Upgrade-Insecure-Requests} 1
- 创建
defined.robot
设置不同类型的header
*** Settings ***Resource variables.robot # 引入资源文件*** Keywords *** # 自定义关键字headers # web header ${dict_headers} Create Dictionary Host=${Host} User-Agent=${User-Agent} Accept=${Accept} Accept-Language=${Accept-Language} Accept-Encoding=${Accept-Encoding} ... Connection=${Connection} Cache-Control=${Cache-Control} Return From Keyword ${dict_headers}api_headers # api header ${api_headers} create dictionary Content-Type=application/x-www-form-urlencoded Return From Keyword ${api_headers}
编写自定义函数
创建py文件 functions.py
,编写自定义函数,用来解决一些自定义的场景。例如,获取json数据,获取加密数据等
# coding=utf-8import json# 获取json串def json_d(**params): return json.dumps(params)
编写测试用例
编写测试用例baidu_auth.robot
*** Settings ***Suite Teardown Delete All SessionsResource ../config/defined.robotLibrary ../py_codes/functions.pyLibrary CollectionsLibrary StringLibrary RequestsLibraryLibrary OperatingSystem*** Test Cases ***Case One #case name Test Baidu Auth ebe4d31d84ffd1d300267f2eceeedecc sasdasdazxczx #与keywords里的arguments数据 一一对应Case Two Test Baidu Auth ebe4d31d84ffd1d300267f2eceeedecc sasdasdazxczx*** Keywords ***Test Baidu Auth [Arguments] ${code} ${uuid} [Tags] baidu auth Create Session httpbin http://www.domian.com/ ${data}= json_d code=${code} uuid=${uuid} #使用自定义的json_d方法 获取json串 ${params}= create dictionary oauth_data=${data} ${header} api_headers ${resp}= Post Request httpbin /cloud_disk/oauth/baiduAccessToken data=${params} headers=${header} should be equal as strings ${resp.status_code} 200 #断言判断http code 是否200 log ${resp.json()} #记录返回结果 should be equal as integers ${resp.json()["status"]} 0 #断言判断接口返回状态 是否为0
- 在
keywords
块中定义请求内容,可以将一个keywords
当成一个函数,Arguments
块就是传入这个函数的内容,在例子中只传入了两个上传给接口的参数。当然也可以将期盼的结果传入到关键字中,然后用断言的方式判断请求的结果是否符合预期。 test cases
则是定义测试内容的地方,配合在keywords
设置的arguments
设置期望传递的参数。框架会根据位置来一一对应参数,同时有多少个case就会发起多少次请求。- 在
settings
块中,定义这个测试需要的资源与库。例如:在之前定义的公有headers
在settings
块中以Resource
的形式引入,而functions.py
则以Library
的形式引入。 - 除了使用定义python方法的形式达到一些自定义操作,之外还可以直接在
robot
文件中执行python函数,例如,需要一个MD5值
${MD5} Evaluate hashlib.md5('hello'.encode(encoding='utf8')).hexdigest() hashlib
使用Evaluate
关键字,执行之后跟随的python代码,在第四个位置带上需要import的包名即可。
执行测试用例
robot cases/baidu_auth.robot # 执行特定的robotrobot cases # 执行cases文件下所有的robot
- 执行之后会生成三个文件,统计测试用例运行的结果已经生成一些日志,方便查看。
- robot 命令还有很丰富的参数,如:指定生成日志的文件的目录、按照tag执行测试用例等