Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Embedding arguments into keyword names 关键字名称中如何嵌套参数
- 在RobotFramework的测试案例中可以把参数嵌入到关键字名称中,使测试案例的可读性更强
- ### User keywords
- 把参数嵌入到关键字名称中
- *** Keywords ***
- Set ${object} state to ${state}
- Log To Console ${object}
- Log To Console ${state}
- </nbsp>
- *** Test Cases ***
- Example test case
- ${status}= Run Keyword And Return Status Set camera state to locked
- Should be True ${status}
- ### Library Keywords
- Library keywords can also accept arguments which are passed using Embedded Argument syntax. The robot.api.deco.keyword decorator can be used to create a custom keyword name for the keyword which includes the desired syntax.
- 在测试库Test Library中利用@keyword修饰符把参数嵌入关键字定义中
- from robot.api.deco import keyword
- @keyword('Add ${quantity:\d+} Copies Of ${item} To Cart')
- def add_copies_to_cart(quantity, item):
- # ...
- </nbsp>
- *** Test Cases ***
- My Test
- Add 7 Copies Of Coffee To Cart
- ## 有关Run Keyword如何获取嵌套关键字内部的错误信息
- 如果在关键字中写了断言,会导致测试案例报错,通过`Run Keyword and Ignore Error`和`Run Keyword and Return Status`执行关键字但可以忽略关键字中的报错,并捕获关键字的执行结果后在测试案例中再处理。
- *** Test Cases ***
- Example test case
- [Tags] example
- ${status} ${values} Run Keyword and Ignore Error Set camera state to locked
- Should Be Equal as Strings ${status} PASS ${values}
- 而在关键字中,还可以通过`run keyword and continue on failure`让错误信息累计起来一起返回,当我们需要查找和反馈出所有错误内容时(例如排查所有输入数据文本中格式错误的内容),可以采用这种方法(把`run keyword and continue on failure`在`FOR`循环体的断言前),但RobotFramework是测试工具,不是数据分析工具,这样的测试案例效率很低,所以这种方法要谨慎使用。
- *** Keywords ***
- Set ${object} state to ${state}
- run keyword and continue on failure Should be equal ${object} 1st${state} ${object}和1st${state}不相同
- run keyword and continue on failure Should be equal ${object} 2nd${state} ${object}和2nd${state}不相同
- Log To Console ${object}
- Log To Console ${state}
- [Return] 'Nothing'
- ### 更好的解决方案
- 在Keyword中不使用`run keyword and continue on failure`,这样在案例中调用时,断言失败则停止。是否可以在测试案例调用关键字时,忽略关键字内部的所有断言失败,但在测试案例中捕获所有的失败信息?
- 显然也是有办法的,看下面的例子:
- *** Keywords ***
- Should Be Equal
- [Arguments] @{args}
- Run Keyword And Continue On Failure BuiltIn.Should Be Equal @{args}
- 因为user keywords 的优先级高于 library keywords, 所以可以用User keywords中的`Should Be Equal`替换`BuiltIn.Should Be Equal`产生失败后仍然可以继续运行的行为。
- 所以,我们就可以把上面的代码预先定义为一个Resouce文件(ContinueOnFailureAssertions.robot),把需要替换的BuiltIn关键字全部包装成失败后可继续运行的,当我们需要我们的测试排查所有错误时,我们就Import 这个Resource文件后再调用我们的字定义关键字。
- 然后,把我们关键字断言前的`Run Keyword And Continue On Failure`去掉。
- *** Keywords ***
- Set ${object} state to ${state}
- Should be equal ${object} 1st${state} ${object}和1st${state}不相同
- Should be equal ${object} 2nd${state} ${object}和2nd${state}不相同
- Log To Console ${object}
- Log To Console ${state}
- [Return] 'Nothing'
- 最后再调用前,先`import resource`。
- *** Test Cases ***
- Example test case log only first Failure
- [Tags] example
- Set camera state to locked
- Example test case log all Failures
- [Tags] example
- log ${CURDIR}
- Import Resource ${CURDIR}/../resources//ContinueOnFailureAsertions.robot
- Set camera state to locked
- 注意上面代码中一个问题,如果两个测试案例执行顺序改变,`Example test case log all Failures`先替换了BuilIn的断言,由于Resource是作用于整个TestSuite的,而不只是作用于TestCase,那么`Example test case log only first Failure`则不会在第一个Failure后停止用例执行。
Add Comment
Please, Sign In to add comment