`

robotframework测试web接口

阅读更多

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 python

from nsp import NSPClient
import sys
import os

def helloworld(uname):
        nsp = NSPClient(****, '***********************');
        svc = nsp.service("nsp.demo")
        ret = svc.helloworld(uname)
        print ret

def lsdir(path):
        nsp = NSPClient('iuTeAN9uaQ6xYuCt8f7uaL4Hwua5CgiU2J0kYJq01KtsA4DY', 'c94f61061b46668c25d377cead92f898');
        svc = nsp.service("nsp.vfs");
        ret = svc.lsdir(path)
        print ret

def 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 python

import os
import sys

class 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 test
        Helloworld      ${NAME}
        Status should be        ${RES1}

Another Test
        Should Be Equal ${NAME} PJ!

VFS Test        [Tags]  nsp.vfs
        Lsdir   "/"
        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.test
        Create Session  vmall   http://api.vmall.com
        ${resp}=        GET     vmall   /rest.php
        Should Be Equal As Strings      ${resp.status_code}     200
        log     ${resp.content}
        Should Contain  ${resp.content} "param"
        ${json}=        To JSON ${resp.content}
        Dictionary Should Contain Value ${json} "param error"
分享到:
评论

相关推荐

    Robot Framework自动化测试修炼宝典

    在第2部分小乘篇中,主要有Web自动化测试、C/S自动化测试、数据库自动化测试、接口自动化测试、RF内置测试库、持续集成自动化测试、移动自动化测试总共七章的内容;在第3部分大乘篇中,主要有自定义你的RF一章的内容...

    robotframework自动化测试demo实例

    robotframework自动化测试demo实例

    Robot Framework

    因为Robot Framework 是灵活和可扩展的,所以它很合适用于测试具有多种接口的复杂软件:用户接口,命令行,web service,编程接口等。 Robot Framework 是开源软件和安装包,源码和相关文档可通过...

    RobotFrameWork3.0中文手册

    RobotFrameWork3.0中文手册 介绍 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架。它是为了端到端的验收测试(End-To-End Acceptance Test)以及验收测试驱动开发(Acceptance-Test-Driven ...

    自动化测试RobotFramework

    web接口自动化,ui自动化的RobotFramework使用及简介 web接口自动化,ui自动化的RobotFramework使用及简介 web接口自动化,ui自动化的RobotFramework使用及简介

    RobotFramework测试框架用例脚本设计方法

    RobotFramework是一个通用的关键字驱动自动化测试框架。测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储。通过测试库中实现的关键字驱动被测软件。RobotFramework灵活且易于扩展。它非常适合测试有...

    Robot Framework 自动化测试框架

    Robot Framework 自动化测试框架,包括接口测试、数据库测试、Web测试、App测试。

    RobotFramework-Selenium2Library1.8中文版

    RobotFramework-Selenium2Library1.8中文版,Ride实现软件测试自动化,使用表格式方法编写测试脚本,调用类库关键字实现接口、功能测试,所测对象包括但不限于web,app,客户端等

    robotframework-2.8.5.win-amd64.exe

    自认为RF是很强大的自动化测试工具,不仅可以可以进行web测试,还可以进行“接口测试”还可以进行app测试。我的同事对RF有个总结:像写代码一项写案例,像写案例一样写代码.

    robotframework-hub:用于访问机器人框架资产的Web应用

    它使用flask提供RESTful接口和基于浏览器的UI来访问测试资产。 很容易上手。 要从PyPi软件包安装并运行,请执行以下操作: $ pip install robotframework-hub $ python -m rfhub 注意:robotframework-hub需要...

    Robot自动化测试环境搭建

    自动化测试环境安装与使用过程,自动化框架:robotframework (兼容anroid、web、ios、接口、数据库等自动化测试) Web库:selenium2 相应浏览器驱动:chromedriver、iedriver (可用于测试浏览器兼容性)

    robotframework-seleniumtestability:SeleniumLibrary的扩展,提供手动和自动等待异步事件,例如提取,xhr等

    机器人框架-Selenium可测试性SeleniumTestability是Robot Framework SeleniumLibrary的插件,无法为其功能添加功能。 这些新功能由SL的插件api存档,然后通过javascript调用自动检测Web应用程序,并提供关键字以将其...

    robot-framework-book-from-ykm.pdf

    robotframework框架本身的学习,以及如何安装;用户手册,实现测试库,常用测试库、扩展库(web自动化、、gui自动化、DB自动化、接口自动化、移动端自动化、敏捷自动化)、框架分析

    AutoZone:web,APP ,接口 UI自动化一体测试平台

    采用Robotframework 的关键字系统,只需要输入关键字 加上元素的定位等方式,减少代码量的编写3.可以自己编写第三库进行导入,扩展性极强4.接口测试与UI测试可以同时进行,采用了python的多进程与协程操作,减少自动...

    js发布会签到系统.zip

    发布会签到系统 完整的发布会签到系统(登录、发布会管理、嘉宾管理、签到功能) 单元测试代码 项目Web接口 接口测试用例 Robot Framework测试脚本

    软件测试基础/ 自动化软件测试值Excel数据驱动

    相关视频涉及Python自动化测试、selenium、appium、jmeter、python、robotframework等。

    持续集成开源工具

    Robot Framework是一款python编写的功能自动化测试框架。具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行。主要用于轮次很多的验收测试和验收测试驱动开发(ATDD...

    基于Python的企业级自动化测试解决方案+源代码+文档说明

    - Robot Framework - pytest - Selenium Webdriver - Appium - Requests - DataTest - numpy - Pandas - scikit-learn and so on 其他开源测试技术框架、工具等等 -------- 该资源内项目源码是个人的毕设,代码都...

Global site tag (gtag.js) - Google Analytics