简体   繁体   English

如何适应 specflow 步骤定义以使用一种方法从 specflow 特征文件中接受不同数量的参数

[英]How to accommodate specflow step definition to accept deferent number of parameters from specflow feature file with one method

Hi I have below Specflow feature files嗨,我有以下 Specflow 功能文件

First scenario第一种情况

 Scenario outline:
    Given Add two numbers <num1> <num2> <num3>
    Then divide by <num4>
    Examples:
    |TestCase|num1|num2|num3|num4|
    |Add     |1   |2   |3   |4   |

Second Scenario:第二种情况:

 Scenario outline:
    Given Add two numbers <num1> <num2>
    Then divide by <num4>
    Examples:
    |TestCase|num1|num2|num4|
    |Add     |1   |2   |4   |

below is the code which I want in Step definition method下面是我在步骤定义方法中想要的代码

[Given(@"Add two numbers (.*) (.*) (.*)")]

[Given(@"Add two numbers (.*) (.*)")] 

public void Testtheconditionwith(string a, string b, string c = null)

{

}

this is not working.这是行不通的。 I don't want to write different methods with different parameters.我不想用不同的参数编写不同的方法。 Is there any way to achieve this?有什么办法可以做到这一点?

Thanks.谢谢。

SpecFlow doesn't support optional parameters in the step definition methods. SpecFlow 不支持步骤定义方法中的可选参数。 You have to write two methods, but you can simply call one from the other.您必须编写两种方法,但您可以简单地从另一个中调用一个。

[Given(@"Add two numbers (.*) (.*) (.*)")]
public void Testtheconditionwith(string a, string b, string c)
{

}

[Given(@"Add two numbers (.*) (.*)")] 
public void Testtheconditionwith(string a, string b)
{
    Testtheconditionwith(a,b,null)
}

Better would be to use the Driver pattern.最好使用 Driver 模式。 More about it at: https://docs.specflow.org/projects/specflow/en/latest/Guides/DriverPattern.html有关它的更多信息,请访问: https://docs.specflow.org/projects/specflow/en/latest/Guides/DriverPattern.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在步骤定义中从SpecFlow功能文件获取路径 - Get the path from a SpecFlow feature file in a Step Definition Specflow:将名称(从功能文件传递字符串)替换为步骤定义文件中的 AccessibilityId - Specflow: Replace Name(pass string from feature file) to AccessibilityId in step definition file 使用步进参数转换时,如何从SpecFlow步进方法中删除多余的参数? - How can I remove superfluous parameters from SpecFlow step method when using step argument transformation? Specflow中的表是否绑定到“步骤定义”或“功能文件”? - Are tables in Specflow tied to Step Definitions or the Feature File? 从SpecFlow中的场景获取当前的步骤定义 - Get current Step definition from of a Scenario in SpecFlow 在Visual Studio 2010中使用specflow创建步骤定义文件时,生成的功能文件不清楚 - Creating the step definition file using specflow in visual studio 2010, the generated feature file is not clear Specflow没有生成正确的步骤定义 - Specflow not generating proper step definition Specflow 步骤文件似乎无法被功能文件识别 - Specflow step file doesn't appear to be recognised by feature file 如何从SpecFlow获取步进时间信息 - How to get step timing information from SpecFlow 无法从SpecFlow步骤定义内部调用Given,When或Then - Can't call Given, When or Then from inside a SpecFlow step definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM