Specflow Scenario Outline不生成预期的步骤代码

IUn*_*own 5 c# bdd specflow gherkin

这是我的小黄瓜:

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 |
Run Code Online (Sandbox Code Playgroud)

这会生成以下步骤文件:

[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();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题如下:

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

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

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

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

AlS*_*Ski 4

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

这意味着您可以去更改它们,而不必担心它们会被破坏:-)

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

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

  • 同意。将自动生成的正则表达式仅视为建议,它并不总是完全正确(自然语言处理很困难)。提示一下,如果您在步骤中将 &lt;username&gt; 和 &lt;password&gt; 等内容加上引号,那么有时会给 SpecFlow 足够的提示,使其生成更合理的结果。例如,“并且用户名“&lt;用户名&gt;”和密码“&lt;密码&gt;”是有效帐户”将导致 SpecFlow 生成带有“[Given(@“用户名””(.*)””和密码“”( .*)""是一个有效的帐户")]`。 (5认同)