Swathi Gona
Swathi Gona

Reputation: 11

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

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.

Upvotes: 0

Views: 1636

Answers (1)

Andreas Willich
Andreas Willich

Reputation: 5835

SpecFlow doesn't support optional parameters in the step definition methods. 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. More about it at: https://docs.specflow.org/projects/specflow/en/latest/Guides/DriverPattern.html

Upvotes: 1

Related Questions