Skip to content

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

psiberknetic
Copy link
Contributor

@psiberknetic psiberknetic commented Dec 11, 2019

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.
image

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.
image

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

@psiberknetic psiberknetic force-pushed the getting-started-with-simple-examples branch from 2b2ce8f to 3c7d24d Compare December 11, 2019 21:14
@psiberknetic
Copy link
Contributor Author

psiberknetic commented Dec 11, 2019

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.

@psiberknetic
Copy link
Contributor Author

psiberknetic commented Dec 11, 2019

Now that we've created scenarios with variables, we can continue to add new scenarios using the variable lines which create new tests without writing any additional code behind.

Additionally, testers writing Gherkin get intellisense style help as they write new scenarios.
image

Hooray for passing tests with VERY little code:
image

Comment on lines +11 to +26
@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
Copy link
Contributor Author

@psiberknetic psiberknetic Dec 12, 2019

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.

Comment on lines +11 to +24
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 |
Copy link
Contributor Author

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.

Comment on lines +9 to +31
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");
}
}
}
Copy link
Contributor Author

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.

@psiberknetic psiberknetic force-pushed the getting-started-with-simple-examples branch from 8d3c0a2 to 321ce61 Compare December 18, 2019 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant