SpecFlow:场景大纲示例

时间:2014-08-19 16:25:35

标签: c# asp.net-mvc specflow acceptance-testing gherkin

我刚开始使用SpecFlow并且非常喜欢这个工具。但是,我正在讨论与示例数据输入相关的一些问题到场景概要。

只是想知道我所面对的是正常的还是有诀窍。

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

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

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

  2. 下一个问题与数据本身有关。如果我有任何字符,如@或&在我的数据中,然后它会在该点分割该数据并将其提供给下一个参数,以便我得到不正确的数据。

  3. 我已将我的代码包含在下方 - 如果有人有任何建议或资源可以查看,我们将不胜感激。

    功能文件         功能: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         | 
    

    步骤生成的文件

    [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();
            }
        }
    

3 个答案:

答案 0 :(得分:15)

默认情况下,

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

当您运行&#34;生成步骤定义时,&#34;你选择了&#34;方法名称 - 下划线&#34;在样式下拉列表中。这样就可以将输入参数解释为SpecFlow,这将创建所谓的“贪婪”和“贪婪”。用于标识参数值的正则表达式。这意味着它将包含逗号作为值的一部分。

您是否已选择&#34;属性中的正则表达式,&#34; (或者手工重构你的代码并手工装饰你的属性)你的步骤可能如下所示:

[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
}

这创造了一个更加节俭的&#39;表达式,告诉SpecFlow接受任何长度的字符串,最多但不包括任何尾随逗号。正则表达式周围的单引号会使它更加明确:

[Given(@"I have completed the form with '(.*)', '(.*)', '(.*)', and '(.*)'")]

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

答案 1 :(得分:3)

已解决 - 使用@或&amp;等字符不是问题。它实际上是在我的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>

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

答案 2 :(得分:0)

如果Cater的答案不能胜任,以供将来参考。我遇到以下问题

Give I have a <typeOfDay> day
When Im asked how I am
Then I will say <feeling>

Example:
|typeOfDay|feeeling|
|good     |happy  |
|bad      |sad    |

您会注意到,由于打字错误,Then语句中的“感觉”将无法找到相应的值。这将导致SpecFlow抛出“输入字符串格式不正确”错误。在我看来,这花了很长时间才能找到。

还有其他需要检查的地方:)