-
Notifications
You must be signed in to change notification settings - Fork 0
Simple SpecFlow Examples #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
2b2ce8f
to
3c7d24d
Compare
In the previous few commits, I've demonstrated some very simple SpecFlow scenarios that add numbers together. This seems like a lot of work to implement multiple scenarios. Let's learn how to make this code simpler by using variables. |
@mytag | ||
Scenario: Check if store is open at 12/11/2019 5:00PM | ||
Given It is currently 12/11/2019 at 5:00PM | ||
Then The store is open | ||
|
||
Scenario: Check if store is open at 12/15/2019 11:00PM | ||
Given It is currently 12/15/2019 at 11:00PM | ||
Then The store is closed | ||
|
||
Scenario: Open time edge case - store opens at 8AM | ||
Given It is currently 12/11/2019 at 8:00AM | ||
Then The store is open | ||
|
||
Scenario: Close time edge case - store closes at 10:00PM | ||
Given It is currently 12/11/2019 at 10:00PM | ||
Then The store is closed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to use Given, When and Then for every scenario. If you don't need one, don't use them. Some tests are simple enough that you don't need to try to fit that template.
In the "Check if the store is open" example above, just the Given and Then lines provide extremely readable test scenarios.
Scenario Outline: Testing multiple dates and times | ||
Given it is a certain <date> and <time> | ||
Then The store <isornot> open at that time | ||
|
||
Examples: | ||
| date | time | isornot | | ||
| 12/20/2019 | 12:00 PM | is | | ||
| 12/12/2019 | 9:00 AM | is | | ||
| 01/03/2019 | 6:00 AM | isnot | | ||
| 12/12/2019 | 11:00 PM | isnot | | ||
| 12/14/2019 | 9:00 AM | isnot | | ||
| 12/14/2019 | 11:00 AM | is | | ||
| 12/12/2019 | 8:00 AM | is | | ||
| 12/12/2019 | 10:00 PM | isnot | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running many scenarios with the same logic can often be simplified by using Scenario Outlines. Scenario outlines allow you to provide a table of examples that will all run through the same check. Here is an example of the Gherkin text for testing whether our store is open at various dates and times.
One advantage of this practice is that new scenarios can be added to the table at any time, and the tests will be generated for them without having to change any code.
public class CheckToSeeIfTheStoreIsOpen_OutlineVersionSteps | ||
{ | ||
DateTime dateToTest; | ||
|
||
[Given(@"it is a certain (.*) and (.*)")] | ||
public void GivenItIsACertainAnd(string p0, string p1) | ||
{ | ||
dateToTest = DateTime.Parse($"{p0} {p1}"); | ||
} | ||
|
||
[Then(@"The store (.*) open at that time")] | ||
public void ThenTheStoreOpen(string p0) | ||
{ | ||
if (dateToTest.IsStoreOpen()) | ||
{ | ||
p0.ToLower().Replace(" ", "").Should().Be("is"); | ||
} | ||
else | ||
{ | ||
p0.ToLower().Replace(" ", "").Should().Be("isnot"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here is the simple code-behind that was implemented to run the scenarios.
8d3c0a2
to
321ce61
Compare
Some code and examples for using SpecFlow in Visual Studio.
Note: This code was built as .NET Core 3.x. Building and running this code requires Visual Studio 2019 with .NET Core 3.x installed. Core 3.x can be installed via the Visual Studio Installer if it is not installed on your system.
Using SpecFlow also requires a one-time installation of the SpecFlow Visual Studio extension. This can be installed using the "Manage Extensions" option in Visual Studio.

To add a SpecFlow test to a test project, right click on the project and select Add, New Item and from the Add New Item screen select SpecFlow Feature File.

Once Gherkin has been written, generate the code behind by right-clicking within the Feature File and select "Generate Step Definitions"
