有关循环的关键字
- 在一系列元素中循环
- 根据数字的范围来循环
- 重复执行某一个关键字多次
最后一个和真正的循环是有差别的,意味着你得将所有操作封装到一个关键字中。并且,在执行结束之前无法退出循环。
语法非常直接,不需要过多解释。唯一需要注意的是,循环体内的关键字必须用 ‘\’ 来进行转义。
测试:
输出:
Starting test: StandardLoopDemo.For-Loop-In-Range20130426 11:24:14.389 : INFO : 120130426 11:24:14.390 : INFO : ${RANDOM_STRING} = B20130426 11:24:14.390 : INFO : B20130426 11:24:14.391 : INFO : 220130426 11:24:14.392 : INFO : ${RANDOM_STRING} = ih20130426 11:24:14.392 : INFO : ih
Ending test: StandardLoopDemo.For-Loop-In-Range
Starting test: StandardLoopDemo.For-Loop-Elements20130426 11:24:14.394 : INFO : @{ITEMS} = [ Star Trek | Star Wars | Perry Rhodan ]20130426 11:24:14.395 : INFO : Star Trek20130426 11:24:14.396 : INFO : ${ELEMENT} = StarTrek20130426 11:24:14.396 : INFO : StarTrek20130426 11:24:14.397 : INFO : Star Wars20130426 11:24:14.398 : INFO : ${ELEMENT} = StarWars20130426 11:24:14.398 : INFO : StarWars20130426 11:24:14.399 : INFO : Perry Rhodan20130426 11:24:14.400 : INFO : ${ELEMENT} = PerryRhodan20130426 11:24:14.400 : INFO : PerryRhodan
Ending test: StandardLoopDemo.For-Loop-Elements
Starting test: StandardLoopDemo.For-Loop-Exiting20130426 11:24:14.402 : INFO : @{ITEMS} = [ Good Element 1 | Break On Me | Good Element 2 ]20130426 11:24:14.402 : INFO : Good Element 120130426 11:24:14.403 : INFO : Do more actions here ...20130426 11:24:14.404 : INFO : Break On Me
Ending test: StandardLoopDemo.For-Loop-Exiting
Starting test: StandardLoopDemo.Repeat-Action20130426 11:24:14.408 : INFO : Repeating keyword, round 1/220130426 11:24:14.408 : INFO : Repeating this ...20130426 11:24:14.408 : INFO : Repeating keyword, round 2/220130426 11:24:14.409 : INFO : Repeating this ...
Ending test: StandardLoopDemo.Repeat-Action
有关条件判断的关键字
在测试代码中使用条件判断会带来不少争议。不用担心,唯一应该记住的是,测试实现应该尽可能简单明了,不要混杂过多条件逻辑。
在下面的例子中将使用以下相关的关键字。
- Run Keyword - 这个关键字将其他关键字作为一个变量传入。这意味着,测试能够动态地改变执行时所使用的关键字,比如执行其他函数返回的关键字。
- Run Keyword If - 在测试复杂结构时非常有用,比如被测的 web 页面在输入不同时会有不同的选项。但是在测试中混有过多的程序结构会使 troubleshooting 变得困难。
- Run Keyword And Ignore Error - 哈,我还没有找到对应的实际例子。
- Run Keyword If Test Failed - 如果测试失败了可以用这个打一些 log,或者打一个快照。在 troubleshooting 时有用。
测试:
输出:
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword20130426 13:34:50.840 : INFO : ${MY_KEYWORD} = Log20130426 13:34:50.841 : INFO : Test
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-If20130426 13:34:50.843 : INFO : ${TYPE} = V120130426 13:34:50.844 : INFO : Testing Variant 1
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword-If
Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error20130426 13:34:50.847 : INFO : @{CAPTAINS} = [ Picard | Kirk | Archer ]20130426 13:34:50.848 : INFO : Length is 320130426 13:34:50.849 : FAIL : '[u'Picard', u'Kirk', u'Archer']' should be empty20130426 13:34:50.850 : INFO : Reached this point despite of error
Ending test: Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error