Question

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

Was it helpful?

Solution

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()
{
     ...

OTHER TIPS

I think you are really missing the point here. You ALWAYS need a When. That is the thing you should be testing! What you can leave out are the Givens

What you should be saying is;

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

Given When Then is really a nicer way of representing a state machine.

Given some initial state // in your case, non
When I perform some action // in your case, visiting the homepage
Then I have some final state // in your case, text displayed to a user

What I like to do is to think about all the things that must be present to allow the When to happen. In your case there doesn't seem to be any initial state. But consider if you had some web application. You would need to have an initial state before visiting the homepage (you'd need to make sure the user is logged in);

Given a user // user must be stored in the database
And the user is logged in // a logged in user must be in the session
When the user visits their homepage
Then the user should see "Welcome To Your Homepage"

An alternative scenario would be;

Given no logged in user // some people would leave this Given out, but I add it for completness
When a user visits their homepage
Then the user should be redirect to the login page

As someone correctly pointed out, most BDD tools don't actually differentiate between Given When Then but you should! The verbose nature of 'Given When Then' has been chosen as its easier for us humans to understand and helps our thought processes. A machine couldn't care less what you call the steps. With this being the case, you should make every effort to utilise the keywords correctly at all times.

Apologies for the thread resurrection...

I'd have probably gone with:

Given there is a homepage
When I view the homepage
Then I should see "Welcome To The Site"

I like to keep at least one Given, When and Then in each Scenario - don't forget you can also use And and But (not that they're particularly relevant to this scenario). You can even just make a bullet-point style list with *.

I would say:

Given I have requested the home page
When the home page loads
Then I should see 'Welcome To The Site'

I tend to see Given as the equivalent of traditional pre-conditions. When as the equivalent of the test action. And Then as the equivalent of the expected result.

Therefore, if there are no pre-conditions, I would leave out Given and simply focus on When and Then:

When I'm on the homepage
Then I should see "Welcome To The Site"

Specflow will allow you to use Given or When, but Visual Studio will also allow you to write a single class that is 1000's of lines long. Just because both are possible, does not mean either is 'right'.

You don't need a When. I like the think of the Given/When/Then keywords like

Given - This is a preparation step, do anything you need to be able to perform the test When - This should be an action that your test will verify. Then - This should be where you verify your test based on the action performed in the When steps.

As previously suggestion they only affect the execution order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top