RF 条件判断、初始化与清除

2020-04-07  本文已影响0人  清水秋香

条件判断
RF里面除了循环以外,另一个流程控制的基本功能就是条件判断
RF中通常用Run Keyword if 关键字来达到累屎Python中if...else...条件判断的功能

image.png
    ${res}  getwebinfo
    run keyword if  "2020" in $res and 'Apr' in $res
    ...  log to console  通过

    run keyword if  "202dfsff" in $res or 'fs' in $res
    ...  log to console  通过
    ...  ELSE IF  '2020' in $res  log to console  xxxx
    ...  ELSE  log to console  没有

    #for 循环不支持嵌套,但是if语句支持嵌套
    run keyword if  "2020" in $res
    ...  run keyword if  'UTC' in $res  log to console  进入里面的循环

循环里的判断
RF中没有while,只能一个大的range
Exit For Loop 关键字实现break功能
用Continue For Loop 关键字实现continue功能,
也可以使用等价的关键字
Continue For Loop If
Exit For Loop If

  :FOR  ${i}  in range  99
        \  ${score}  get value from user  请输入成绩
        \  run keyword if  $score == "contiue"  continue for loop
        \  run keyword if  $score == 'exit'  exit for loop
        \  run keyword if  int($score) > 60   log to console  及格了
        ...  ELSE  log to console  不及格

    :FOR  ${i}  in range  99
        \  ${score}  get value from user  请输入成绩
        \  continue for loop if  $score == "contiue"
        \  exit for loop if  $score == 'exit'
        \  run keyword if  int($score) > 60   log to console  及格了
        ...  ELSE  log to console  不及格

evaluate
在python里面调用eval执行的关键字有:should be true 和 run keyword if
evaluate关键字:直接用python代码表达式来生成一个结果

${var}  Create List  hello  word
等价于
${var}  evaluate  ['hello','word']

setup和teardown
setup是测试一个用例(或者套件)前要做的事情,
teardown是测试后要做的事情。

[Setup]   log to console  生成用例2所需数据
[Teardown]  log to console  清除用例2产生数据
${var}  create list  hello  word
append to list   ${var}   a  b  c
log to console   ${var}

测试套件文件的setup、teardown
除了测试用例,测试套件也有初始化清除。
有的操作是针对一个套件里面的所有的用例的,而且只需要做一次
测试套件文件有两种类型的setup和teardown
一个是Suite setup/teardown,一个是 Test setup/teardown
Suite setup/teardown进入和退出这个suite执行用例前后必须执行且只分别执行一次
Test setup/teardown如果suite内的用例本身没有setup/teardown,才执行

#初始化和清除都会遵守一个原则就是那个近就用哪个
#为何需要套件级别的初始化和清除 10条测试用例,用户名和密码,如果不用套件级别的就需要每运行一个用例就执行一遍初始化和清除,用例执行效率会下降
#在做初始化清除的时候套件里面的所有用例所依赖的数据环境是一样的,就可以把生成数据的动作放在套件里面,但是套件里面产生的数据不能对其他套件产生干扰所以就要
#在当前套件执行完之后把数据清除掉
Suite Setup  log to console  产生套件一所需数据
Suite Teardown  log to console  清除套件一产生数据
#用例中没有初始化和清除,就会使用"默认的"
Test Setup  log to console   ---Test setup
Test Teardown  log to console  ---Test teardown

测试套件目录的setup、teardown
测试套件目录也可以有初始化和清除。
测试套件目录的setup teardown 在其目录下的配置文件__ init__.txt或者 __ init__.robot 里的settings表里
也有两种类型的setup和teardown
一个是Suite setup/teardown,一个是 Test setup/teardown
Suite setup/teardown进入和退出这个suite执行用例前后必须执行且只分别执行一次
Test setup/teardown如果suite内的用例或者子套件本身没有setup/teardown,才执行

image.png
上一篇 下一篇

猜你喜欢

热点阅读