带有 Gherkin 的机器人框架中的场景大纲

Posted

技术标签:

【中文标题】带有 Gherkin 的机器人框架中的场景大纲【英文标题】:scenario outline in robot framework with Gherkin 【发布时间】:2017-12-01 18:51:42 【问题描述】:

我想在 Robot Framework 中以最“优雅”的方式将多个参数传递给我的测试用例。许多框架中使用的场景大纲允许用表中的值替换变量/关键字。表格中的每一行都被视为一个场景。

Robot Framework 中的场景大纲是否有任何等价物?

例如我们有这样的测试用例:

Given users email is $email
And users password is $password
When user is on login page
Then user can sign in 

例如,我们希望将多个电子邮件和密码作为一个数组传递给这个测试用例,并运行与该数组中的行一样多的次数。我可以在不将测试用例转换为关键字以使用模板的情况下做到这一点吗?

【问题讨论】:

测试用例不带参数。您能否更清楚地了解“将参数传递给我的测试用例”是什么意思?你问test templates怎么用? 我添加了一个例子来说明我的意思,我希望现在已经清楚了。 :) 【参考方案1】:

您不能将 BDD 样式的测试与数据驱动的测试混用。最接近的方法是以 BDD 样式编写关键字,然后在实际测试用例中使用该关键字。

例如,您可以这样做:

*** Keywords ***
users email is 
    [Arguments]  $email
    set test variable  $current email  $email

users password is
    [Arguments]  $password
    set test variable  $current password  $password

User is on login page
    log  pretend we are on the login page

User can sign in
    should contain  $current password  !
    ...  password for $current email doesn't contain '!' ($current password)
    ...  values=False

User login template
    [Arguments]  $email  $password

    Given users email is  $email
    And users password is  $password
    When user is on login page
    Then user can sign in

*** Test Cases ***
Scenario: user can sign in with valid credentials
    [Template]  User login template

    # username        # password
    larry@example.com   heymo!
    moe@example.com     wiseguy!
    curly@example.com   nyuknyuk!

【讨论】:

好的,我知道这是在机器人框架中执行此操作的方法。但是请看这个例子:link 不是将数据驱动测试与 BDD 混合在一起吗?对我来说,这是一个相当合理的解决方案,而在机器人框架中这似乎很不清楚。但我是这个主题的初学者,我可能会弄错。 ;) @Ewa:该链接指向不同的测试框架。黄瓜和机器人是两个完全不同的东西。每个测试框架中有很多事情是其他测试框架无法做到的。机器人框架从未设计为支持黄瓜语言 gherkin。 是的,我知道这些是不同的框架。我的问题是我是否可以在机器人框架中做类似场景大纲的事情。我想现在我知道这不是完全可能的。谢谢你的回答。 :)【参考方案2】:

请参阅这篇优秀的blog,了解如何在 Robot Framework 中使用类似 Gherkin 的数据表(这是一个优雅的解决方法)。

这是示例代码:

*** Settings ***
Force Tags        OnlyThisOne
Resource          BDD.robot

*** Test Case ***     \          Closed Period             Open Period               Run Import On    Old Manager Stops    New Manager Starts
1                     Example    1.11.2009 - 30.11.2009    1.12.2009 - 31.12.2009    11.11.2009       30.11.2009           1.12.2009

2                     Example    1.11.2009 - 30.11.2009    1.12.2009 - 31.12.2009    1.11.2009        31.10.2009           1.11.2009

3                     Example    1.11.2009 - 30.11.2009    1.12.2009 - 31.12.2009    1.12.2009        30.11.2009           1.12.2009

*** Keyword ***
Example
    [Arguments]    $periodClosed    $periodOpenAndModified    $importDay    $oldManagerValidUntil    $newManagerValidFrom
    Given initialized criteria for bonus commercial
    And a branch B with branch manager M_OLD and employee E1
    And evaluation for E1 for period $periodClosed which is closed
    And evaluation for E1 for period $periodOpenAndModified which is open and modified
    When M_NEW becomes new manager of branch B
    And import service is called on $importDay
    Then the new branch manager of branch B is M_NEW valid from $newManagerValidFrom
    And branch manager M_OLD manages employee E until $oldManagerValidUntil
    And branch manager M_NEW manages employee E from $newManagerValidFrom
    And Evaluations for E1 still have the same content
    Given initialized criteria for bonus commercial
    But M_NEW becomes new manager of branch B
    But import service is called on $importDay
    And M_OLD becomes new manager of branch C

BDD.robot 资源文件:

*** Settings ***
Documentation     Library de.codecentric.fourtexx.robot.ModelKeyword
#*** Test Cases ***
#Branch, Manager and Worker
#    Given a branch A with branch manager Boss and employee Worker
*** Keywords ***
the new branch manager of branch $branch is $manager valid from $newManagerValidFrom
    assert branch manager valid from    $branch    $manager    $newManagerValidFrom
initialized criteria for bonus commercial
    No Operation
a branch $branch with branch manager $manager and employee $employee
    No Operation
evaluation for $employee for period $periodClosed which is closed
    No Operation
evaluation for $employee for period $periodOpenAndModified which is open and modified
    No Operation
$manager becomes new manager of branch $branch
    No Operation
import service is called on $importDay
    No Operation
branch manager $manager manages employee $employee until $oldManagerValidUntil
    No Operation
branch manager $manager manages employee $employee from $newManagerValidFrom
    No Operation
Evaluations for $employee still have the same content
    No Operation
assert branch manager valid from
    [Arguments]    $arg1    $arg2    $arg3
    Evaluate    True

【讨论】:

【参考方案3】:
*** Test Cases ***
Scenario: eating cucumbers
    [Template]  Scenario Outline: eating cucumbers

    # Examples:
    # start  eat  left
    12       5    7
    20       5    15
    33       11   22
    35       15   20

*** Keywords ***
Scenario Outline: eating cucumbers
    [Arguments]  $start  $eat  $left
    Given there are $start cucumbers
    When I eat $eat cucumbers
    Then I should have $left cucumbers

字体:https://gist.github.com/Tset-Noitamotua/8f06bd490918a56b0485630016aef60b

【讨论】:

以上是关于带有 Gherkin 的机器人框架中的场景大纲的主要内容,如果未能解决你的问题,请参考以下文章

带有变量分配的机器人框架中的 IF ELSE

Robot 框架 Gherkin 风格中的参数语法

大纲机器学习神经网络算法在建筑学中的应用

错误:没有部分:使用 DatabaseLibrary 的机器人框架中的“默认”

带有 appium 的机器人框架(无法识别元素)

如何在机器人框架中访问套件中的所有测试