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
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.
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).
Yes, more than one When / Then cycle is appropriate in a Gherkin scenario when the real-world scenario calls for it.
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.
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() { ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With