robotframework 是一个简单易用的关键字驱动自动化测试框架,我这里用dbank的python的sdk作为目标测试程序简单使用robotframework

Why Robot Framework?

  • Enables easy-to-use tabular syntax for creating test cases in a uniform way.
  • Provides ability to create reusable higher-level keywords from the existing keywords.
  • Provides easy-to-read result reports and logs in HTML format.
  • Is platform and application independent.
  • Provides a simple library API for creating customized test libraries which can be implemented natively with either Python or Java.
  • Provides a command line interface and XML based output files for integration into existing build infrastructure (continuous integration systems).
  • Provides support for Selenium for web testing, Java GUI testing, running processes, Telnet, SSH, and so on.
  • Supports creating data-driven test cases.
  • Has built-in support for variables, practical particularly for testing in different environments.
  • Provides tagging to categorize and select test cases to be executed.
  • Enables easy integration with source control: test suites are just files and directories that can be versioned with the production code.
  • Provides test-case and test-suite -level setup and teardown.
  • The modular architecture supports creating tests even for applications with several diverse interfaces.

首先安装并检测是否成功

pip install robotframework
ciaos@linux-53dr:~/Downloads/python-sdk-0.1> pybot --version
Robot Framework 2.7.6 (Python 2.7.3 on linux2)

修改demo.py为下面的代码(作为目标测试接口,命令行程序)

nspApi.py

#!/usr/bin/env pythonfrom nsp import NSPClient
import sys
import osdef helloworld(uname):nsp = NSPClient(****, '***********************');svc = nsp.service("nsp.demo")ret = svc.helloworld(uname)print retdef lsdir(path):nsp = NSPClient('iuTeAN9uaQ6xYuCt8f7uaL4Hwua5CgiU2J0kYJq01KtsA4DY', 'c94f61061b46668c25d377cead92f898');svc = nsp.service("nsp.vfs");ret = svc.lsdir(path)print retdef help():print 'Usage: %s { nsp.demo.helloworld | nsp.vfs.lsdir | help }' \% os.path.basename(sys.argv[0])if __name__ == '__main__':actions = {'nsp.demo.helloworld': helloworld, 'nsp.vfs.lsdir': lsdir,'help': help}try:action = sys.argv[1]except IndexError:action = 'help'args = sys.argv[2:]try:actions[action](*args)except (KeyError, TypeError):help()

按照robotframework规范编写测试库如下

testLib/NspLibrary.py

#!/usr/bin/env pythonimport os
import sysclass NspLibrary:def __init__(self):self._nsp_path = os.path.join(os.path.dirname(__file__),'..', '.', 'nspApi.py')self._status = ''def helloworld(self, name):self._run_command('nsp.demo.helloworld', name)def lsdir(self, path):self._run_command('nsp.vfs.lsdir',path)def status_should_be(self, expected_status):if expected_status != self._status:raise AssertionError("Expected status to be '%s' but was '%s'"% (expected_status, self._status))def _run_command(self, command, *args):command = '"%s" %s %s' % (self._nsp_path, command, ' '.join(args))process = os.popen(command)self._status = process.read().strip()process.close()

编写测试用例nspTest.tsv(必须以单个分割符\t区分关键字,不然会报错,不能用多个\t或者空格)

*** Settings ***
Library OperatingSystem
Library testLib/NspLibrary.py*** Variables ***
${NAME} "PJ!"
${RES1} "{'message': 'Hello:PJ!'}"
${LSDIR_RES}    "{'childList': {0: {'type': 'Directory', 'name': 'Netdisk'}, 1: {'type': 'Directory', 'name': 'Syncbox'}, 2: {'type': 'Directory', 'name': 'Recycle'}}}"*** Test Cases ***
Demo Test       [Tags]  nsp.demo[Documentation] Example testHelloworld      ${NAME}Status should be        ${RES1}Another TestShould Be Equal ${NAME} PJ!VFS Test        [Tags]  nsp.vfsLsdir   "/"Status should be        ${LSDIR_RES}Lsdir   "/Netdisk"Status should be        "Wrong results"

运行pybot nspTest.tsv,结果如下

ciaos@linux-53dr:~/Downloads/python-sdk-0.1> pybot nspTest.tsv
==============================================================================
nspTest
==============================================================================
Demo Test :: Example test                                             | PASS |
------------------------------------------------------------------------------
Another Test                                                          | PASS |
------------------------------------------------------------------------------
VFS Test                                                              | FAIL |
Expected status to be 'Wrong results' but was '{'childList': {0: {'type': 'Directory', 'name': 'zhujhdir'}, 1: {'type': 'Directory', 'name': '\xe7\x85\xa7\xe7\x89\x87'}, 2: {'type': 'Directory', 'name': '\xe6\x96\x87\xe6\xa1\xa3'}, 3: {'type': 'Directory', 'name': '\xe6\x88\x91\xe6\x94\xb6\xe5\x88\xb0\xe7\x9a\x84\xe6\x96\x87\xe4\xbb\xb6'}, 4: {'type': 'Directory', 'name': '\xe6\x88\x91\xe7\x9a\x84\xe6\x96\x87\xe4\xbb\xb6'}, 5: {'type': 'Directory', 'name': '\xe8\xbd\xaf\xe4\xbb\xb6'}, 6: {'type': 'File', 'name': '\xe6\x88\x91\xe7\x9a\x84\xe6\xb5\x8b\xe8\xaf\x95.php'}, 7: {'type': 'File', 'name': 'data.txt'}, 8: {'type': 'File', 'name': 'test.thrift'}, 9: {'type': 'File', 'name': 'acds4.txt'}, 10: {'type': 'File', 'name': 'nsp_sdk.log'}, 11: {'type': 'File', 'name': 'data.rar'}, 12: {'type': 'File', 'name': 'test.rar'}}}'
------------------------------------------------------------------------------
nspTest                                                               | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================
Output:  /home/ciaos/Downloads/python-sdk-0.1/output.xml
Log:     /home/ciaos/Downloads/python-sdk-0.1/log.html
Report:  /home/ciaos/Downloads/python-sdk-0.1/report.html

其中report.html等文件包含详细的测试结果

此外robotframe包含丰富的测试工具,如内置测试库以及用于测试web接口的requests库,直接使用request测试库可以免去我们编写web请求的工作。

安装顺序

pip install -U requests==0.9.0

pip install -U robotframework-requests

使用示例:

WEB TEST        [Tags]  web.testCreate Session  vmall   http://api.vmall.com${resp}=        GET     vmall   /rest.phpShould Be Equal As Strings      ${resp.status_code}     200log     ${resp.content}Should Contain  ${resp.content} "param"${json}=        To JSON ${resp.content}Dictionary Should Contain Value ${json} "param error"

robotframework测试web接口相关推荐

  1. 基于jmeter测试web接口,看完都说学会了

    最近接到一个需求,产品说要对一个接口做负载均衡.当时我听到这个需求的时候,我的内心是奔溃的--这接口只有一个,怎么做负载均衡,负载均衡起码得有两个才能做啊! 最后理解了产品想要做的东西:由于线上某接口 ...

  2. postman测试登录后的接口_【使用Postman测试web接口】Postman的安装与入门

    最近在做Web API开发,开发完成后,需要对API进行自测,自测通过后才能checkin到代码库.之前进行web接口测试的时候,使用过Chrome浏览器的一个插件--Postman,觉得很好用,方便 ...

  3. 一款功能强大的Web接口和网页测试工具

    小编在浏览网上技术文章的时候接触到了一款Web接口和网页测试工具:Postman.在此之前小编做接口测试时经常使用命令行的方式进行接口测试,但尝试使用了这个工具之后不禁为这款工具强大的功能所折服,所以 ...

  4. 失业在家抠脚的我花了2个月,读完了这份《Python Web接口开发与测试》,我居然进华为了...

    学习计划 失业在家抠脚到华为年薪25w测试工程师,我只花了2个月~ 底层逻辑 如果要进大厂,算法.底层.项目经验都要刷,小编以后会给大家更新各种面试题-- 如果要进大厂,项目经验.底层算法.网络.数据 ...

  5. 《Web接口开发与自动化测试 -- 基于Python语言》 ---前言

    前    言 本书的原型是我整理一份Django学习文档,从事软件测试工作的这六.七年来,一直有整理学习资料的习惯,这种学习理解再输出的方式对我非常受用,博客和文档是我主要的输出形式,这些输出同时也帮 ...

  6. 利用SoapUI 测试web service的一些问题总结

    总结两个利用SoapUI 测试web service的一些问题: 1.请求一个soap service 请求的时候:按照下面的配置输入请求地址后, 2.根据实际service接口的需要,传入相应的参数 ...

  7. WEB接口测试之Jmeter接口测试自动化 (一)(初次接触)

    软件测试自动化从不同的测试阶段分类,可从下层到上层依次分为单元测试-->接口测试-->界面自动化测试. 单元测试一般有开发人员自行完成,而界面自动化测试合适的测试条件又很难达到,测试人员在 ...

  8. python前端接口_Python接口自动化——Web接口

    1.2.1 web接口的概念 这里用一个浏览器调试工具捕捉课程管理页面请求作为例子: 当请求页面时,服务器会返回资源,将协议看做是路的话,http可以看做高速公路,soap看做铁路传输的数据有html ...

  9. 利用SoapUI 测试web service的方法介绍

    http://boyun.sh.cn/blog/?p=1076 1. 简介 SoapUI是用java开发的测试web service的工具. 2. 安装 2.1. 下载地址 http://www.so ...

最新文章

  1. 周立功-成功心法(2):通过讲故事营销自己
  2. 挤牙膏机器,实话,没啥用
  3. 边沿检测—以脉冲形式给出信号
  4. 牛客-沙漠点列【tarjan】
  5. lightoj1145 【DP优化求方案】
  6. C++中 list与vector的区别
  7. 【AI视野·今日Robot 机器人论文速览 第八期】Wed, 16 Jun 2021
  8. python求组合数_求组合数的算法_Cppowboy's Blog - SegmentFault 思否
  9. npm install vs. update - 有什么区别?
  10. atitit 数字音频技术概论 艾提拉著 目录 1. 声学基础 2 1.1. 1.2人耳的听觉效应9 2 2. 第1章数字音频基础 2 2.1. 1.1音频的发展历史 2 2.2. 1.2音频的发展
  11. 基于FFmpeg的视频播放器之九:使用SDL2播放音频
  12. 鼠标失灵c语言代码,[转载]键盘和鼠标操作失灵代码
  13. vue实现文字翻转效果
  14. 程序员深爱的bilibili后台源码泄露,看哔哩哔哩官方回应才放心了
  15. Claus Hansen加入Entrust Datacard,担任亚太地区和日本销售副总裁
  16. 互联网早报|宠物行业上半年融资吸金超60亿;猿辅导、掌门教育进军素质教育赛道
  17. 《Redis设计与实现》第十一章 AOF持久化
  18. 【AUTOSAR】【CAN通信】CanTrcv
  19. 数据库安全性定义与检查
  20. ssi=spring3+struts2+ibatis2

热门文章

  1. Android Pixel手机Notification小图标显示白方块问题
  2. Scrapy 规则化爬虫(1)——CrawlSpider及link_extractor
  3. BZOJ3837: [Pa2013]Filary
  4. GIS领域常用软件工具(框架)介绍与推荐
  5. 如何在el-table中如何使用计算属性computed
  6. (转载)计算机视觉当中的专业英语
  7. 计算机网络安全技术保护措施,计算机网络安全技术保护措施
  8. vue-router 面试题
  9. 思维导图 XMind 闯关之路(第05关)插入外框概要
  10. 程序设计入门——C语言 翁恺 第1周编程练习