How do describe a simple process in Gherkin-style?

1.4k views Asked by At

Suppose I'm designing some SaaS service. And I need to have a function that allow users create sites. User can makes special settings for each site in the admin panel (e.g. design of widget) and gets unique code for install service to his own site.

User story could be:

As a logged user I want to add the new site in the admin panel so that I could configure each instance of widgets separately and could get a unique code for install widget to my own site.

Form

But if I will try to describe this functionality with BDD or GWT (Given When Then) or Gherkin-style, I will face with some trouble. I start from next description:

GIVEN I'm logged into admin panel AND I'm on "Sites" page

WHEN I click "Add site" button

THEN Pop-up window "Add site" come up

As you can see above realization suppose that site adding will be in pop-up window (e.g. it very important for UX). Pop-up window contains Site URL input field, drop-down control with languages and "Add" and "Cancel" buttons.

And we got a strange scenario which responsible for just pop-up opening. Is it correct? And how can I name this scenario ("Add site's form opening" ??)? Also this scenario has only a one case (when I click - pop-up open). Maybe this scenario not needed at all? I'm confused...

In this case we need to create another scenario when describes:

GIVEN "Add site" pop-up form is opened

WHEN I fill the "Site URL" field AND click on "Add" button

THEN New site will be create in system AND I will transfer to my own site's list

How do you think, where do I need to apply a business rules such as: 1) When new site is created a unique code must be generated and consist of minimum 8 characters including numbers and Alphabetical symbols. 2) checks doesn't apply for Site URL input field and user can input a Cyrillic symbols 3) etc?

I have a lot of additional question and hope on the community help!

2

There are 2 answers

4
Marc Lainez On BEST ANSWER

The thing with BDD is to stay away from implementation details as much as possible. This scenario has multiple implementation details:

GIVEN I'm logged into admin panel AND I'm on "Sites" page
WHEN I click "Add site" button
THEN Pop-up window "Add site" come up
  • What happens if the "Sites" page becomes "Awesome Site" page or is simply deleted?
  • What happens if "Add site" is not a button anymore?
  • What happens if it's not a popup but a redirect occuring
  • What happens after? Is the value simply in showing the popup? I guess not...

For this specific example, a better approach would be:

GIVEN I'm an authorised administrator
WHEN I enter all the required information for a new site and save it
THEN I should see that site in my own sites list

With this scenario, if your implementation changes, you will only have to change your step definitions, you won't have to change your gherkin. Don't forget that those tests are supposed to explain the Behaviour of the system, not the way it is implemented.

The other questions you have are more related to unit testing in my opinion:

  1. When new site is created a unique code must be generated and consist of minimum 8 characters including numbers and Alphabetical symbols => I'd do it at the class level, gherkin would not be appropriate unless the customer specifically asked for this, then the condition is "THEN a code having the required characteristics is generated for that site" and you would have to define "required characteristics" in a glossary the customer can read and understand.

  2. Checks doesn't apply for Site URL input field and user can input a Cyrillic symbols => again, would put that at the class level en same as for 1. unless the customer wants to be able to read something about it, it should be at the unit level.

I hope that answers your question. I recommend this article by Dan North if you want to have a better idea of how to write better gherkin features.

EDIT 11/13/14

Based on your comments, I suggest we take a step back and describe a way to deal with requirements in your case. I have to tell you that I'm not a BDD expert and am only sharing my own personal experience, for more info on the subject I suggest you get in touch with the guys behind BDD Kickstart and Cucumber.pro where you will find online BDD courses. They will be able to give you lots of information and books to read.

That being said, let's dive into the subject.

The first thing you get is a list of features or stories that, if you follow Mike Cohn's story template would look like:

As a <type of user> I want <to do something> in order to <serve a business purpose>

I personally like to put the business purpose first to make sure we don't skip it in the discussions. You might also not follow that template and that's fine, but remember that it's a good idea to make sure the features you are listing with your customers have a business purpose. If there is no business value behind a feature then what's the point of doing it anyway...

So you do have a list of features/stories described as above. Now for each of these features, there are different cases or scenarios, that's what Dan describes in his article. This is where the Given-When-Then is introduced.

Scenario: Title
Given <some context>
When <there is an event>
Then <something happens>

Each of those scenarios are examples on how this specific feature behaves in different contexts. They are the different acceptance criteria for a specific feature, things the customer described as the expected behaviour of the system. They should be ignorant of any implementation details. So stuff like:

Given I am on page "First page"
When I click "Hello world"
Then I should see "You clicked hello world"

Is wrong for the reasons described prior to this edit.

Let's assume the following feature:

In order to save time when answering clients requests, as a webmaster, 
I want to be able to manage the list of websites I am responsible for

Scenarios for this story would be:

Scenario 1: Show a list of websites
GIVEN I am an authorised administrator
AND I am managing several websites
THEN I should see a list of all the sites I manage

Scenario 2: Add website to list
GIVEN I am an authorised administrator
WHEN I enter all the required information for a new site and save it
THEN I should see that site in my own sites list

Scenario 3: Edit website from list
GIVEN I am an authorised administrator
WHEN I edit the site informations
THEN I the changes should be visible in my sites list

...

Now what if you want to go into data validation stuff like "site should have a title" for instance. To me there are two different ways to approach this. You can test that from the user's perspective with a full-stack test or test that there is some validation at the object level.

Let's assume the following scenario:

Scenario: New site has no title
GIVEN I'm an authorised administrator
WHEN I forget to fill in the title for a new site and save it
THEN I should be warned the site is not valid

You can use cucumber or specflow to run this scenario from the UX, therefore using some kind of browse-based proxy to test your app. That is usually slow as it hits the whole system and simulates a real user. It's an option, but I don't think it's the best though. IMO not all tests should be run against the UX and having too many Gherkin features can be a pain to maintain, that's why I prefer focusing on having the happy or critical path (usually I ask myself, where does the money comes from) tested full-stack and put the rest at lower levels.

You can still use Gherkin for these unit tests if you'd like. But that is not mandatory. You only need a way to show your customer you actually have a test for all those specific format controls and validation checks.

That doesn't mean you are not doing BDD anymore, you can still use the given-when-then-should pattern in rspec if you're a rubyist, or any other testing framework you use.

Hope that clarifies all this, let me know if there are any confusing parts...

0
jbpros On

I think Marc simply deserves the big green tick on this one, thanks to his amazingly thorough answer!

I just wanted to add a few comments.

  1. You don't need to automate all your scenarios.

If you want to capture business requirements in a form that everyone (i.e. including non-tech savvy folks) can understand and Gherkin's Given/When/Then work for you, just go for it. There's nothing forcing you to automate all of your scenarios.

  1. You don't need to automate all your scenarios through the UI.

Your software is made of layers that often respond to similar behaviours, via different interfaces (UI, HTTP, API, ...). Should you want to describe fine-grained business rules (i.e. site name constraints) with automated gherkin scenarios, you could write step definitions that talk directly to your domain layer instead of going through the user interface. That would probably still give you a decent level of confidence.

As a side note, I would recommend not to use Given/When/Then in classic testing frameworks (i.e. those that only devs can see!) if your purpose is to share your tests/requirements with non-tech people.

  1. Have conversations!

Above all, BDD is about better communication: try to talk more, involve your developers (or some of them) earlier in the process so that they gain more knowledge, sooner. Formalising Gherkin scenarios comes in a second phase. Automating them should even be further down your priority list!