天天看點

測試自動化——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即為測試的結果

繼續閱讀