robot framework基础操作

2018-12-29  本文已影响0人  聪明的石头

robotframework支持的文件格式

HTML

TSV

Plain TEXT

reStructuredText


robotframework目录文件结构

RF是以project为单位进行管理的:

<> 一个project可以包含多个Test Suite

<>一个Test Suite可以包含多个 test case

<>一个Test Suite有四部分组成:Setting,Variables,Test Case,Keywords


执行脚本

执行整个项目所有用例,pybot项目路径:pybot D:\robot

执行整个Test Suite用例,pybot suite 路径:pybot D:\robot\testsuite.robot

执行单个测试用例:pybot --case名称 该用例所在suite的路径,如:pybot --case_login in D:\robot\testsuite.robot

将测试结果输出到固定路径:pybot --outputdir 报告路径  用例路径,如:pybot  outputdir D:test  D:\robot\testsuite.robot

执行tag的测试用例:pybot --include tag name 项目路径,如:pybot --include nonal  D:\robot


查看报告文件

用例执行完成 之后会形成三个文件,log.html ,out.xml,repot.html

报告截图

output.xml:记录的测试结果是XML文件,根据特定的需要可以编写脚本读取XML文件并生成特定的测试报告

log.html:会记录RF运行的每一个步骤,查看脚本运行的每一个步骤

report.html:测试报告,整体性展示运行状况


整体结构展示及关键字说明

Setting 下的列举的是为整个测试套件写的

*** Setting ***

Documentation 这个是当前Test Suite说明文字

Library                当前Test Suite需要使用的库

Resource                当前Test Suite需要加载使用的资源,可能是参数也可能是用例文件

Metadata                定义元数据

Variables              引用变量文件

Suite Setup            Test Suite执行前的动作

Suite Teardown          Test Suite执行后的动作

Test Setup              Test Case执行前的动作

Test Teardown          Test Case执行后的动作

Force Tags              Test Suite下的所有测试用例都会被打上这个tag

Default Tags            Test Suite的用例如果没有打上tag,就会用这个默认tag,如果打了tag,就会用自己的tag

Test Timeout            设置每一个测试用例的超时时间,只要超过这个时间就会失败,并停止案例运行

...                    这是防止某些情况导致案例一直卡住不动,也不停止也不是失败

Test Template          数据驱动模板(很有用的一个参数)


*** Variables ***

${scal}   创建变量

@{list}    创建列表

&{dict}    创建字典


*** Test Case ***

Test_01

[Documentation]      测试用例说明……

[Template]                数据驱动模板,每条用例只有一个模板

[Tags]                       测试用例标签

[Setup]                     测试用例执行前的动作

[Teardown]              测试用例执行后的动作

[Timeout]                 测试用例的超时时间

My Keyword One

Test_02

[Documentation] Post Request With URL Params

Create Session    httpbin                       http://httpbin.org

 ${params}=         Create Dictionary       key = value        key2=value2

 ${resp}=               Post Request            httpbin                /post                  params = ${params}

 Should Be Equal As Strings            ${resp.status_code}        200

*** Keywords ***

My Keyword One

[Documentation]      关键字描述

[Arguments]             自定义参数设置

[Return]                    将返回值抛出

[Timeout]                  关键字流程执行超时时间

[Tags]                        标签

[Teardown]                关键字流程结束动作

log                     ${SCALAR_VARS}

log Mang           @{LIST_VARS} log ${DICT_VARS}

几个简单的案例:

案例1:practice_setup_and_teardown.robot
*** Settings ***

Documentation          test

Suite Setup          suitestart

Suite Teardown        suitestop

Test Setup               testsetup

Test Teardown          teststop

*** Variables ***

${a}      hello

${b}      world

*** Test Case ***

[Documentation]     case1

case1

log     ${a}

case2

log     ${b}

*** Keywords ***

suitestart

log  suitestart

suitestart2

log  suitstop

案例2:practice_scalar.robot


*** Setting ***

Documentation   RobotFramework脚本的scalar标量练习

Force Tags          robot-3.0

Default Tags        owner-damao

*** Variables ***

#创建scalar变量
${name}    robot framework

${version}  3.0.1

${robot}      ${name}  ${version}

${null_var}   # 字符串被赋予空值

${example}   This value is joined together with a space

#创建列表

@{name}  Matti   Maap

@{list}    @{name}    NONA

@{account}  1 2 3

#创建字典

&{info}   name=Matti   addr = PuDianRoad  phone = 132

&{Info2}  name = Maap  addr = JinQiao  phone = 256

*** Test Case ***

测试打印scalar变量

[Documentation] 打印scalar变量

To Test Scalar

*** Keywords ***

[Documentation] 创建关键字 To Test Scalar [Documentation] 打印标量log${NAME}

To Test Scalar

              log  ${ROBOT}

              log  ${NULL_VAR}

              log  ${EXAMPLE}

              log ${LONG_WORDS}

API Requests

          [Arguments]    ${key}  ${Content-Type}  ${url}   ${api}  ${body}  ${Authorization}

          Return Value   ${value}

         ${headers}   Create Dictionary  ${Content-Type}  ${Authorization}

         ${body_data}   Create Dictionary   ${body}

         Create Session     url  ${url}   headers=${headers}

         ${addr}   Post Request   url   ${api}    data = ${body_data}   headers = ${headers}

         log   ${addr.content}

         log    ${addr.json()}

        ${responsedata}   To Json   ${addr.content}

        ${value}    Get From Dictionary     ${responsedata}   ${key}dd

案例3 :practice_List.robot


*** Setting ***

Documentation       RobotFramework脚本"列表"参数的练习

Force tags              "列表"参数的练习

Default tags             列表

*** Variables ***

@{data_list}            1 2 3 4 5 6

@{data2_list}          a b c d e f

...                            g h

*** Test Case ***

Test_01

Documentation   打印列表

print list variable

Test_02

Documentation   获取列表长度

get  list length

*** Keywords ***

Print List Variable

log many    @{data_list}   # 打印列表元素要使用 log many

log many    @{data2_list}  

get list length

[documentation] 获取列表长度

${length}    Built.in get Length   @{data2_list}

log many     ${length}

案例4:practice_dict.robot

*** Setting ***

Documentation   字典练习

Force Tags         robot 3.0

Default Tags     NONA

*** Variables ***

&{dict}  a=1 b=2 c=3 d=4

&{diact2}  m=1 n=2 g=3 k=4

...               l =5

*** Keywords ***

print dictionary data

log    &{dict}

log    &{dict}[a]

log many  &{dict}[a]   &{dict}[b]

*** Test Case ***

Documnetation   打印字典及元素

print dictionary data

上一篇 下一篇

猜你喜欢

热点阅读