How do I do if statements in Cucumber scenarios?
My app has permissions, and certain tests need to not be run when a particular permission is on, and some tests need to be run when that same permission is on.
Is there开发者_StackOverflow中文版 a way to do this? or do I need to use a different framework?
The standard way of excluding 'tests' in Cucumber is to add a tag to them to identify them, then when invoking Cucumber you specify which tags to include/exclude. In your example, you could tag a specific scenario:
@needs_permission
Scenario: View users billing information
Or tag the whole feature:
@needs_permission
Feature: Administrative area
Scenario: View users billing information
Or tag certain examples in a scenario outline:
Scenario Outline: Visit a page
Given I visit "<page>"
Examples: Don't need permission
| page |
| index |
| sitemap |
@needs_permission
Examples: Do need permission
| page |
| admin |
Now, when you run Cucumber, you can exclude those tags if necessary:
When the permission is on and you want to run all tests:
cucumber .
When the permission is off and you want to exclude the tests that need it:
cucumber . -t ~@needs_permission
An alternative which I have used with mixed results, if you really don't know ahead of time, is to mark a step as pending
if it doesn't apply given the current scenario, e.g.
Given /^I visit some page which needs permission$/ do
pending "Permissions aren't enabled - skipping" unless permissions_enabled?
end
This will mark the step as 'pending' which really means 'not fully implemented', which isn't ideal, especially if you have many such steps - it can be difficult to ensure that other unimplemented steps don't accidentally creep in and get hidden by all the ones you've deliberately marked as such.
We get a similar instance of this a lot, we have 2 solutions, either tags as suggested, or create different scenarios for different permission types:
Given i log in as a moderator
and there is a new post
when i view the post
Then i should see a delete button
Given i log in as a normal user
and there is a new post
when i view the post
Then i should not see a delete button
May be possible using tags. Difficult to say without more detail.
精彩评论