0

I'm new to cucumber and currently stuck trying to organize/structure some features/scenarios. The behavior I'm trying to capture is that of users, guests and admins using my website. The website is basically for creating private and public todo lists.

And these are the specific scenarios I want to test:

  • Registered users can view their private todo lists (but not someone else's private todo list)
  • Guests (anonymous users) can only view public todo lists
  • Admins can view all lists
  • Managing todo lists

Right now I'm going with something like this:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "user-one@email.com" with password "secret-one"
  Given a user named "user-two@email.com" with password "secret-two"
  Given an admin named "admin@email.com" with password "admin"

Scenario: user-one@email.com can view his own private todo lists
Scenario: user-one@email.com can not view user-two@email.com private todo lists
Scenario: admin@email.com can view user-one@email.com and user-two@email.com private todo lists
Scenario: user-one@email.com can create private todo lists
Scenario: user-one@email.com can delete todo lists they own
Scenario: admin@email.com can delete user-one@email todo list
Scenario: guests can view all public todo lists

The problem I'm running into is the setup for each scenario. For example, in the first scenario I have to assume user-one@email.com is logged in. In the first admin@email.com scenario I have to assume the admin is logged in. In the last scenario I need to assume no one is logged in.

So how do I manage those Givens? Do I just add a

Given user-one@email.com is logged in

To every single scenario? Or is there a better way to go about structuring all this? Please help! I'm sure this is a very common pattern that cucumber users need to test.

Lan
  • 6,039
  • 7
  • 22
  • 24

1 Answers1

1

Generally I try to list only the steps that are important. In your case I would do something like:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "user-one@email.com" with password "secret-one"
  Given a user named "user-two@email.com" with password "secret-two"
  Given an admin named "admin@email.com" with password "admin"

Scenario: Users can see own private todo lists
  Given user "user-one" has a private todo list
  Then user "user-one" can see the private todo list of "user-one"

Scenario: Users cannot see others private todo lists
  Given user "user-one" has a private todo list
  Then user "user-two" cannot see the private todo list of "user-one"

If you have a login step elsewhere you can re-use that step in the Then user "user-one" can see the private todo list of "user-one" step defintion, rather than in the feature. Or your step could just share the same code directly.

See step re-use

Community
  • 1
  • 1
Pat L
  • 538
  • 2
  • 7
  • Could you please show me an example of re-using a step? I think this is exactly what I'd like to do – Lan Jan 19 '11 at 04:15
  • I edited the answer with a link to http://stackoverflow.com/questions/918066/reuse-cucumber-steps – Pat L Jan 19 '11 at 05:02