Guest User

Untitled

a guest
Jun 18th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. ## Embedding arguments into keyword names 关键字名称中如何嵌套参数
  2. 在RobotFramework的测试案例中可以把参数嵌入到关键字名称中,使测试案例的可读性更强
  3. ### User keywords
  4. 把参数嵌入到关键字名称中
  5.  
  6. *** Keywords ***
  7. Set ${object} state to ${state}
  8. Log To Console ${object}
  9. Log To Console ${state}
  10. </nbsp>
  11.  
  12. *** Test Cases ***
  13. Example test case
  14. ${status}= Run Keyword And Return Status Set camera state to locked
  15. Should be True ${status}
  16.  
  17. ### Library Keywords
  18. 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.
  19.  
  20. 在测试库Test Library中利用@keyword修饰符把参数嵌入关键字定义中
  21.  
  22. from robot.api.deco import keyword
  23.  
  24. @keyword('Add ${quantity:\d+} Copies Of ${item} To Cart')
  25. def add_copies_to_cart(quantity, item):
  26. # ...
  27. </nbsp>
  28.  
  29. *** Test Cases ***
  30. My Test
  31. Add 7 Copies Of Coffee To Cart
  32.  
  33. ## 有关Run Keyword如何获取嵌套关键字内部的错误信息
  34.  
  35. 如果在关键字中写了断言,会导致测试案例报错,通过`Run Keyword and Ignore Error`和`Run Keyword and Return Status`执行关键字但可以忽略关键字中的报错,并捕获关键字的执行结果后在测试案例中再处理。
  36.  
  37. *** Test Cases ***
  38. Example test case
  39. [Tags] example
  40. ${status} ${values} Run Keyword and Ignore Error Set camera state to locked
  41. Should Be Equal as Strings ${status} PASS ${values}
  42.  
  43. 而在关键字中,还可以通过`run keyword and continue on failure`让错误信息累计起来一起返回,当我们需要查找和反馈出所有错误内容时(例如排查所有输入数据文本中格式错误的内容),可以采用这种方法(把`run keyword and continue on failure`在`FOR`循环体的断言前),但RobotFramework是测试工具,不是数据分析工具,这样的测试案例效率很低,所以这种方法要谨慎使用。
  44.  
  45. *** Keywords ***
  46. Set ${object} state to ${state}
  47. run keyword and continue on failure Should be equal ${object} 1st${state} ${object}和1st${state}不相同
  48. run keyword and continue on failure Should be equal ${object} 2nd${state} ${object}和2nd${state}不相同
  49. Log To Console ${object}
  50. Log To Console ${state}
  51. [Return] 'Nothing'
  52.  
  53. ### 更好的解决方案
  54. 在Keyword中不使用`run keyword and continue on failure`,这样在案例中调用时,断言失败则停止。是否可以在测试案例调用关键字时,忽略关键字内部的所有断言失败,但在测试案例中捕获所有的失败信息?
  55.  
  56. 显然也是有办法的,看下面的例子:
  57.  
  58. *** Keywords ***
  59. Should Be Equal
  60. [Arguments] @{args}
  61. Run Keyword And Continue On Failure BuiltIn.Should Be Equal @{args}
  62.  
  63. 因为user keywords 的优先级高于 library keywords, 所以可以用User keywords中的`Should Be Equal`替换`BuiltIn.Should Be Equal`产生失败后仍然可以继续运行的行为。
  64.  
  65. 所以,我们就可以把上面的代码预先定义为一个Resouce文件(ContinueOnFailureAssertions.robot),把需要替换的BuiltIn关键字全部包装成失败后可继续运行的,当我们需要我们的测试排查所有错误时,我们就Import 这个Resource文件后再调用我们的字定义关键字。
  66.  
  67. 然后,把我们关键字断言前的`Run Keyword And Continue On Failure`去掉。
  68.  
  69. *** Keywords ***
  70. Set ${object} state to ${state}
  71. Should be equal ${object} 1st${state} ${object}和1st${state}不相同
  72. Should be equal ${object} 2nd${state} ${object}和2nd${state}不相同
  73. Log To Console ${object}
  74. Log To Console ${state}
  75. [Return] 'Nothing'
  76.  
  77. 最后再调用前,先`import resource`。
  78.  
  79. *** Test Cases ***
  80. Example test case log only first Failure
  81. [Tags] example
  82. Set camera state to locked
  83.  
  84. Example test case log all Failures
  85. [Tags] example
  86. log ${CURDIR}
  87. Import Resource ${CURDIR}/../resources//ContinueOnFailureAsertions.robot
  88. Set camera state to locked
  89.  
  90. 注意上面代码中一个问题,如果两个测试案例执行顺序改变,`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