SpecFlow:场景大纲示例

cco*_*ker 12 c# asp.net-mvc acceptance-testing specflow gherkin

我刚开始使用SpecFlow并且非常喜欢这个工具.但是,我正在讨论与方案大纲中的示例数据输入相关的一些问题.

只是想知道我所面对的是正常的还是有一个技巧.

我正在使用C#Visual Studio 2013并使用下划线样式的步骤定义编写MVC应用程序.我也尝试过正则表达式样式,但仍然遇到类似的问题.

所以问题是我提供了用户名,密码等作为参数,并在我的示例中包含了示例数据.看来会发生以下情况: -

  1. 当第一次生成场景时,我必须在参数周围放置"",否则它根本不会被作为参数拾取.但是当从示例中传入数据时,我在传入数据的末尾得到一个"/".当我回到场景时,然后删除参数周围的"".这有点令人沮丧,但如果这是处理它的最佳方式,我可以忍受.只是想知道是否有人对这一点有任何建议.

  2. 下一个问题与数据本身有关.如果我的数据中有任何字符(如@或&等),那么它会在该点分割该数据并将其提供给下一个参数,这样我就会得到不正确的数据.

我已经在下面包含了我的代码 - 如果有人有任何建议或资源可以看,我将不胜感激.

功能文件 功能:AccountRegistration为了在我的组织中使用Mojito服务作为访客用户,我想创建一个具有管理privelages的帐户

Scenario Outline: Register with valid details
    Given I am on the registration page
        And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
    When I have clicked on the register button
    Then I will be logged in as <username>
        And my account will be assigned the role of <role>

        Examples: 
        | email     | organisation | password  | passwordConfirmation | username  | role  |
        | usernamea | Bytes        | password1 | password1            | usernamea | Admin |
        | usernameb | Bytes        | password2 | password2            | usernameb | Admin |
        | usernamec | Bytes        | password3 | password3            | usernamec | Admin |
        | usernamed | Bytes        | password4 | password4            | usernamed | Admin |
        | usernamee | Bytes        | password5 | password5            | usernamee | Admin |

Scenario Outline: Register with invalid details
    Given I am on the registration page
        And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
    When I have clicked on the register button
    Then I will get an error message

        Examples: 
        | email             | organisation    | password   | passwordConfirmation | 
        | Jonesa@mojito.com | Bytes           | 1LTIuta&Sc | wrongpassword      | 
        | Jonesb@mojito.com | Bytes           | 1LTIuta&Sc | 1LTIuta&Sc         | 
        | Jonesc@mojito.com | No Organisation | 1LTIuta&Sc | 1LTIuta&Sc         | 
Run Code Online (Sandbox Code Playgroud)

步骤生成的文件

[Binding]
    public class AccountRegistrationSteps
    {
        [Given]
        public void Given_I_am_on_the_registration_page()
        {
            ScenarioContext.Current.Pending();
        }

        [Given]
        public void Given_I_have_completed_the_form_with_usernamea_Bytes_password_P0_and_password_P1(int p0, int p1)
        {
            ScenarioContext.Current.Pending();
        }

        [Given]
        public void Given_I_have_completed_the_form_with_Jonesa_mojito_com_Bytes_P0_LTIuta_Sc_and_wrongpassword(int p0)
        {
            ScenarioContext.Current.Pending();
        }

        [When]
        public void When_I_have_clicked_on_the_register_button()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_I_will_be_logged_in_as_usernamea()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_my_account_will_be_assigned_the_role_of_Admin()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_I_will_get_an_error_message()
        {
            ScenarioContext.Current.Pending();
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 15

默认情况下,SpecFlow 处理字符串参数,问题是您在运行时确定您的值是什么时将控制权交给SpecFlow.

运行"生成步骤定义"时,在"样式"下拉列表中选择了"方法名称 - 下划线".这样就可以将输入参数解释为SpecFlow,这将创建所谓的"贪婪"正则表达式来识别参数值.这意味着它将包含逗号作为值的一部分.

如果你选择了"属性中的正则表达式"(或者重构你的代码并手工修饰你的属性),你的步骤可能如下所示:

[Given(@"I have completed the form with (.*), (.*), (.*), and (.*)")]
public void Given_I_have_completed_the_form_with(string email, string org, string pwd, string conf)
{
    //do stuff here
}
Run Code Online (Sandbox Code Playgroud)

这会创建一个更"简约"的表达式,告诉SpecFlow接受任意长度的字符串,最多但不包括任何尾随逗号.正则表达式周围的单引号会使它更加明确:

[Given(@"I have completed the form with '(.*)', '(.*)', '(.*)', and '(.*)'")]
Run Code Online (Sandbox Code Playgroud)

自己管理正则表达式可能会让人头痛,但如果你这样做,它确实暴露了SpecFlow的全部功能.


cco*_*ker 6

已解决 - 使用 @ 或 & 等字符不是问题。它实际上在我的 Given Statement 中使用了逗号。我发现如果我使用“和”,它会起作用。所以为了让它工作,声明必须写成如下: -

解决方案

  1. 将语句写为

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

  2. 修改语句以在需要为字符串的参数周围加上单引号

    Given I have completed the form with '<email>' and '<organisation>' and '<password>' and '<passwordConfirmation>'

  3. 生成步骤定义,然后将语句改回以排除单引号

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

有点乱,但它得到了正确的结果。希望将来 SpecFlow 将更新为将参数作为字符串处理为默认值。

  • 实际上 [specflow 文档](http://specflow.org/documentation/Using-Gherkin-Language-in-SpecFlow/) 说:*在占位符(例如 '&lt;placeholder&gt;' )周围放置单引号 (') 不是强制性的到 Gherkin 语言,但有助于提高 SpecFlow 解析场景大纲的能力。*第 3 步在此处已过时,无需删除引号即可正常工作。 (3认同)