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

14.7k views Asked by At

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

6

There are 6 answers

5
perfectionist On BEST ANSWER

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()
{
     ...
1
Ryan Burnham On

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.

0
SaxonMatt On

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

0
Charlie S On

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

0
QATester1 On

I would say:

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

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.

Additional

BDD structure is no different to a well set-out test with arrange, act, assert.

The benefit of BDD though is that it gives a verbose structure to tests. This helps devs have domain appropriate conversations with product owners - Behaviour Driven Development.

If you aren't having these conversations, there's very little value in using BDD over normal tests practices.