【问题标题】:How to accommodate specflow step definition to accept deferent number of parameters from specflow feature file with one method如何适应 specflow 步骤定义以使用一种方法从 specflow 特征文件中接受不同数量的参数
【发布时间】:2021-01-25 16:44:52
【问题描述】:

您好,我有以下 Specflow 功能文件

第一个场景

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

第二种情况:

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

下面是我想要的步骤定义方法中的代码

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

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

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

{

}

这不起作用。我不想用不同的参数编写不同的方法。 有什么方法可以实现吗?

谢谢。

【问题讨论】:

  • 请使用问题编辑器中的格式代码图标格式化您的代码。太难读了。
  • 请勿发布代码图片。将其复制并粘贴到问题编辑器中。

标签: c# specflow


【解决方案1】:

SpecFlow 不支持步骤定义方法中的可选参数。 您必须编写两个方法,但您可以简单地调用另一个方法。

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

最好使用驱动模式。更多信息请访问:https://docs.specflow.org/projects/specflow/en/latest/Guides/DriverPattern.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多