Given When Then Testing - Do I NEED a "When"?

Dan Black picture Dan Black · Jan 27, 2012 · Viewed 9.3k times · Source

I am implementing some smoke tests to our website.

I'm using a Given/When/Then format for existing automated acceptance tests/User stories. But now I want to do an initial smoke test of:

Given I'm on the homepage

Then I should see "Welcome To The Site"

Am I missing something? Is it "ok" to not have a When?

Tools Used: MVC3, SpecFlow, Nunit, Watin

Answer

perfectionist picture perfectionist · Jan 27, 2012

It is completely valid syntax to omit any of Given, When or Then (and even to mix them in any order - specflow doesn't care.)

However, for the purpose of readability, rather than omit the When I often rephrase the Given, i.e.

When I view the homepage
Then I should see "Welcome To The Site"

I prefer to omit the Given section, because the When is supposed to indicate what the tested action is.

If the code for the step binding is the same and you want to re-use it, you can always bind your given and my when to the same method.

[Given(@"I'm on the homepage"]
[When(@"I view the homepage"]
public void NavigateToHomePage()
{
     ...