【问题标题】:Specflow Scenario Outline not generating expected step codeSpecflow 场景大纲未生成预期的步骤代码
【发布时间】:2013-12-12 00:53:59
【问题描述】:

这是我的小黄瓜:

Scenario Outline: Login using valid email address
Given I have not logged into the current computer
And Username <username> and password <password> is a valid account
When I start the client
And Login using username <username> and password <password>
Then The client should navigate to first time screen

Examples: 
| username         | password    |
| valid001@xyz.com | password001 |
| valid002         | password002 |

这会生成以下步骤文件:

[Binding]
public class UserLoginSteps
{
    [Given(@"I have not logged into the current computer")]
    public void GivenIHaveNotLoggedIntoTheCurrentComputer()
    {
        ScenarioContext.Current.Pending();
    }

    [Given(@"Username valid(.*)@xyz\.com and password password(.*) is a valid account")]
    public void GivenUsernameValidXyz_ComAndPasswordPasswordIsAValidAccount(int p0, int p1)
    {
        ScenarioContext.Current.Pending();
    }

    [When(@"I start the client")]
    public void WhenIStartTheClient()
    {
        ScenarioContext.Current.Pending();
    }

    [When(@"Login using username valid(.*)@xyz\.com and password password(.*)")]
    public void WhenLoginUsingUsernameValidXyz_ComAndPasswordPassword(int p0, int p1)
    {
        ScenarioContext.Current.Pending();
    }

    [Then(@"The client should navigate to first time screen")]
    public void ThenTheClientShouldNavigateToFirstTimeScreen()
    {
        ScenarioContext.Current.Pending();
    }
}

问题如下:

  • 为用户名生成的正则表达式基于示例列中的第一个条目。这不是我想要的,因为并非所有示例都遵循这种模式。
  • 方法名称使用示例列中的第一个条目(即 WhenLoginUsingUsernameValidXyz_ComAndPasswordPassword)。这很难阅读,如果示例表中的数据发生更改,则方法名称不再正确
  • 参数的类型是 int。它们应该是字符串。

我希望步骤定义生成输出如下内容:

    [When(@"Login using username (.*) and password (.*)")]
    public void WhenLoginUsingUsernameAndPassword(string p0, string p1)
    {
        ScenarioContext.Current.Pending();
    }

我错过了什么吗?有没有办法影响 SpecFlow 为场景大纲生成步骤方法的方式?如果没有,在不破坏我的更改背后的功能生成代码的情况下解决此问题的最佳方法是什么

【问题讨论】:

    标签: c# bdd specflow gherkin


    【解决方案1】:

    简而言之,好消息。这些自动生成的绑定只是为了方便,不会自动重新生成。事实上,我一直都是自己手工制作的。

    这意味着你可以去改变它们而不必担心它们被破坏 :-)

    每次编辑 *.feature 文件时,SpecFlow 都会自动生成 *.feature.cs 文件。这些永远不应该被编辑,但是如果你仔细查看它们,你会发现它们基本上从你的场景中获取 Given、When 和 Then 行,并将它们作为参数传递给它自己的内部方法。在幕后,SpecFlow 使用反射来查找所有具有[Binding] 属性的类,然后查看所有具有[Given][When][Then] 属性的类方法以查找列表正则表达式。最佳匹配的正则表达式标识要调用的方法。这就是为什么绑定经常被描述为全局并且不应该真正使用继承来构建(因为您最终会得到多个相同的正则表达式)。

    回到您所拥有的生成绑定,这些仅在 SpecFlow 的 VS 插件检测到您没有匹配的正则表达式并且您尝试导航到功能文件中的定义 (F12) 时在编辑器中创建。它们确实是您构建的占位符。

    【讨论】:

    • 同意。将自动生成的正则表达式视为一个建议,它并不总是完全正确(自然语言处理很难)。一个提示,如果您在步骤中将 之类的引号括起来,那么它有时会为 SpecFlow 提供足够的提示,使其产生更合理的结果。例如And Username "&lt;username&gt;" and password "&lt;password&gt;" is a valid account 将导致 SpecFlow 生成带有[Given(@"Username ""(.*)"" and password ""(.*)"" is a valid account")] 的步骤。
    【解决方案2】:

    您应该使用单引号来表示 SpecFlow 必须使用整个字符串而不是数字。

    Examples: 
    | username           | password      |
    | 'valid001@xyz.com' | 'password001' |
    | 'valid002'         | 'password002' |
    

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 2016-07-13
      • 2018-08-02
      • 1970-01-01
      • 2016-02-13
      • 2015-07-25
      • 2018-07-25
      • 2014-10-12
      • 2021-04-29
      相关资源
      最近更新 更多