天天看点

测试自动化——robotframework

作为进入某公司测试自动化相关小组的新人,经过了一个多月的学习基本上手这个框架。于是,也就深知经历
           

这个过程的痛苦,尤其是网上的资料不是很多,尤其是中文的资料更是匮乏的难以想象。于是就算简单的想快速的

入个门,快速的知道这个框架是怎么工作的都不行,只能从英文的RobotFramework User Guide这本英文文档向下走

,虽然有一个中文版本的Guide,但是版本有点老了。于是对有些不是很适应。所幸从英文下手了。

既然你已经了解了这个主题,那么你基本已经知道robotframework是个什么东西了。于是就不长篇累牍的介绍

这个框架了。本文的主要内容基本参照Version 2.8.5 RobotFramework User Guide.html()的下边直接进入正文。

本文主要讲述依据python的windows安装(linux操作系统的安装类似),不是jython。不符合你需要求的观众可以

现在走开。

一、安装

1、python的2.7.x版本是推荐版本。并把python的安装目录设置到PATH环境变量,比如c:\Python27和c:\Python27\Scripts

2、安装pip或者easy_install,百度之即可,这是python先关包的管理工具。

3、安装robotframework

# Install the latest version

pip install robotframework

easy_install robotframework

# Upgrade to the latest version
    pip install --upgrade robotframework
    easy_install --upgrade robotframework

    # Install a specific version
    pip install robotframework==2.7.1
    easy_install robotframework==2.7.1

    # Uninstall -- only supported by pip
    pip uninstall robotframework
至此,安装完成。
           

二、编写测试用到的关键字(Python代码)

建立一个目录robot,在此目录下建立自己的关键字所在的库文件(Python源代码)

#CreateName.py
    import logging
    class CreateName:
        def __init__(self):
            self._loger = logging.getLogger(__name__)
            self._loger.setLevel(logging.DEBUG)
        def create_name(self, name):
            print '*WARN* Warning from a library.'
            print '*HTML* This is <b>bold</b>.'
            return name + 'q'
           

三、编写测试用例文件

在robot目录下新建立测试用例所在文件tests.txt文件,内容如下:

* Settings *

Documentation A test suite with a single test for valid login.

… This test has a workflow that is created using keywords in

… the imported resource file.

Library           CreateName
Library           OperatingSystem


*** Variables ***
${message}        hello world
${USER}           qcq
@{many}     hello world ni hao
${name}     qc

*** Test Cases ***
Hello
    Should be equal    hello world    ${message}
    log     ${scalar}
    log many     @{list1}

    :For    ${item}     in      @{many}
    \      Log     ${item}

    Create File    ${CURDIR}${/}qin.txt    I am Chinese
    Should Be Equal     ${0b1011}	${11}
    Qcq equal   ${name}


*** Keywords ***
Qcq equal
    [Documentation]     This is the first keyword write by myself
    ...     If there is sth wrong, just tell me.
    [Arguments]     ${name}
    ${res} =    Create name     ${name}
    Should Be Equal     ${res}  qcq    
    [Teardown]      Log     Die for you
           

四、运行开始测试

在robot目录下执行如下命令即可。

pybot -L trace tests.txt

五、查看测试结果

在目录下生成的log.html即为测试的结果

继续阅读