-
Notifications
You must be signed in to change notification settings - Fork 0
Calling Steps from Step Definitions
Editor note: We recommend reading this documentation entry at http://www.specflow.org/documentation/Calling-Steps-from-Step-Definitions. We use the GitHub wiki for authoring the documentation pages.
It is possible to call steps from Step Definitions:
[Binding]
public class CallingStepsFromStepDefinitionSteps : Steps
{
[Given(@"the user (.*) exists")]
public void GivenTheUserExists(string name)
{
// ...
}
[Given(@"I log in as (.*)")]
public void GivenILogInAs(string name)
{
// ...
}
[Given(@"(.*) is logged in")]
public void GivenIsLoggedIn(string name)
{
Given(string.Format("the user {0} exists", name));
Given(string.Format("I log in as {0}", name));
}
} Invoking steps from step definitions is practical if you have several common steps that you want to perform in several scenarios, or simply if you want to make your scenarios shorter and more declarative. This allows you to do the following in a Scenario:
# feature Scenario: View last incidents Given Linda is logged in # This will in fact invoke 2 step definitions When I go to the incident page
Instead of having a lot of repetition:
# feature Scenario: View last incidents Given the user Linda exists And I log in as Linda When I go to the incident page
Note: When using this approach to remove duplications from your feature files, the console output will contain both the master step and the delegated steps as follows:
Given Linda is logged in Given the user Linda exists And I log in as Linda When I go to the incident page
Sometimes you want to call a step that has been designed to take Multiline Step Arguments, for example:
[Given(@"an expense report for (.*) with the following posts:")]
public void GivenAnExpenseReportForWithTheFollowingPosts(string date, Table postTable)
{
// ...
}This can easily be called from a plain text step like this:
# feature Given an expense report for Jan 2009 with the following posts: | account | description | amount | | INT-100 | Taxi | 114 | | CUC-101 | Peeler | 22 |
But what if you want to call this from a step definition? There are a couple of ways to do this:
[Given(@"A simple expense report")]
public void GivenASimpleExpenseReport()
{
string[] header = {"account" , "description", "amount"};
string[] row1 = {"INT-100" , "Taxi", "114"};
string[] row2 = {"CUC-101" , "Peeler", "22"};
var t = new Table(header);
t.AddRow(row1);
t.AddRow(row2);
Given("an expense report for Jan 2009 with the following posts:", t);
}