Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 698
Dan Black Avatar asked Jan 27 '12 10:01

Dan Black


People also ask

How do you write a test case when given then?

The Given-When-Then formula is a template intended to guide the writing of acceptance tests for a User Story: (Given) some context. (When) some action is carried out. (Then) a particular set of observable consequences should obtain.

What is Given-When-Then called?

Given-When-Then is a style of representing tests - or as its advocates would say - specifying a system's behavior using SpecificationByExample. It's an approach developed by Daniel Terhorst-North and Chris Matts as part of Behavior-Driven Development (BDD).

Can we use when after then in gherkin?

Yes, more than one When / Then cycle is appropriate in a Gherkin scenario when the real-world scenario calls for it.

Can we have multiple given in cucumber?

It can contain one or more Given steps, which are run before each scenario, but after any Before hooks. A Background is placed before the first Scenario / Example , at the same level of indentation.


2 Answers

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() {      ... 
like image 184
perfectionist Avatar answered Oct 05 '22 23:10

perfectionist


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.

like image 28
GWed Avatar answered Oct 06 '22 01:10

GWed