【问题标题】:How to apply data-driven concept to user keywords in Robot Framework?如何将数据驱动的概念应用于 Robot Framework 中的用户关键字?
【发布时间】:2014-06-03 05:21:15
【问题描述】:

假设我为以下操作定义了用户关键字: 发送电子邮件 READ_EMAIL FLAG_EMAIL 我想在 Robot Framework 中随机执行 10 次这些操作。我怎样才能最好地做到这一点?

例如,当我运行这个测试几次时,Robot Framework 可以执行类似的操作 运行 1:SEND_EMAIL。 READ_EMAIL、READ_EMAIL、FLAG_EMAIL、READ_EMAIL、SEND_EMAIL、FLAG_EMAIL、SEND_EMAIL、FLAG_EMAIL、READ_EMAIL 运行 2:READ_EMAIL、FLAG_EMAIL、SEND_EMAIL、FLAG_EMAIL、SEND_EMAIL、FLAG_EMAIL、READ_EMAIL、SEND_EMAIL、FLAG_EMAIL、SEND_EMAIL

我希望 Robot Framework 有这样的东西,但我似乎没有找到任何方法来编写下面的“从 3 个可能的操作中选择 1 个”:

从 1 到 10 的 For 循环: 从 3 个可能的操作中选择 1 个(SEND_EMAIL、READ_EMAIL、FLAG_EMAIL)

【问题讨论】:

  • 这如何适用于数据驱动的概念?

标签: templates keyword robotframework


【解决方案1】:

这样试试:

*** Settings ***

Library  String

*** Test Cases ***

Perform Test Once
    Perform Test

Perform Test Twice
    Perform Test
    Perform Test


*** Keywords ***

SEND_EMAIL
    Log SEND_EMAIL  WARN

READ_EMAIL
    Log READ_EMAIL  WARN

FLAG_EMAIL
    Log FLAG_EMAIL  WARN

Call Random
#   Generate Random String With Length 1 From Numbers 1,2 and 3
    ${random}=  Generate Random String  1  123
#   Call Keyword Randomly Using If/Else If/Else
    Run Keyword If  ${random} == 1  SEND_EMAIL
    ... ELSE IF     ${random} == 2  READ_EMAIL
    ... ELSE        FLAG_EMAIL

Perform Test
#   Call Keyword "Call Random" 10 Times Using FOR loop
    :FOR  ${number}  IN RANGE  0  10
    \   Call Random

【讨论】:

    【解决方案2】:

    你可以使用Evaluate关键字让python返回一个范围内的随机数。然后,您可以使用该数字从关键字列表中选择一个关键字。这样,很容易编写一个从列表中随机选择一个关键字并运行该关键字的关键字

    一旦你有了它,你可以使用循环来做你想要的多次。

    例如:

    *** Test Cases ***
    | Run random email keywords ten times
    | | :FOR | ${counter} | IN RANGE | 10
    | | | Run one of these keywords randomly:
    | | | ... | SEND_EMAIL | READ_EMAIL | FLAG_EMAIL
    
    
    *** Keywords ***
    | Run one of these keywords randomly:
    | | [Arguments] | @{args}
    | | [Documentation] | Run a random keyword from a list of keywords
    | | 
    | | # Get a random number to use to pick a keyword
    | | ${index}= | Evaluate | random.randint(0, len(${args})-1) | random
    | | 
    | | # Get a keyword using the random number
    | | ${keyword}= | Get variable value | ${args[${index}]}
    | | 
    | | # Run the keyword
    | | Run keyword and continue on failure | ${keyword}
    

    请注意,这将作为单个测试用例而不是 10 个测试用例出现在日志中。您可以选择让所有 10 个关键字都像示例中那样运行,也可以在第一个关键字失败时失败。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2016-09-17
      • 2011-03-02
      • 2013-08-15
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2011-06-08
      • 2017-07-07
      相关资源
      最近更新 更多