Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

cucumber - Is it possible to reuse a feature as the "Given" for another feature?

Is it possible to reuse a feature as the "Given" for another feature?

Or am I trying to do something I shouldn't be trying to do

basically my features look like:

Scenario: Creating a basic account with valid details (happy path)
  Given I am on the "signup" page
  And I enter all the right details #this is shortened of about 20 steps for your reading ease
  When I press the button labelled "Sign up"
  Then I should see the text "Thanks for signing up"
  And I should have an email from "confirmation@mysite.com" titled "Confirm your account Michael"
  And the database should contain a record for the user marked as unconfirmed

Scenario: Confirming account via email
  Given I have created a basic account
  When I open the confirmation email and visit the url to confirm the account
  Then I should be logged in
  And the database should contain a record for the user makred as confirmed

I clear my DB after every feature as they should all be able to be run individually...

Am I going about this the wrong way?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Problem

What you're actually attempting is to reuse a scenario. This is no longer supported in Cucumber.

Aside from other problems with this approach, your tests will be slower and interdependent, since you will be:

  1. driving account creation through a browser, and
  2. making all your tests dependent on the account-creation test passing.

Don't do that.

The Cucumber Way

Generally, you should write your tests to work independently, although you can certainly reuse step definitions. So, in the general case, you might want to add shared steps like:

  1. Given that user account "Test User" does not exist
  2. Given that user account "Test User" exists

which can then be included in your scenarios as needed. The nice thing about this approach is that the steps can create or delete users programmatically.

Alternatively, if most of your tests will be on existing accounts, set up the default data set with the right user fixtures already in place. For the limited subset where you will be testing account creation, just add a scenario background that drives user deletion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...