简体   繁体   中英

Background in capybara tests using cucumber

Learning new Rails application and writing some tests for it. While writing tests, question came across is :

What is the meaning of writing 'Background' in capybara tests using cucumber? Why do we write 'Background' at all? Can it be avoided?

-> Does it have anything to do with the test database? Or Does it serve as the database for the "Scenario"?

Feature: User signup
  As a user
  I want to sign in
  So I can use service features

**Background**:
  Given user with "jack@daniles.com" email and "qwerty" password

Scenario: Signing in with correct credentials
  When I go to sign in page
  And I fill in "email" with "jack@daniles.com"
  And I fill in "password" with "qwerty"
  And I click "Login" button
  Then I should see "Welcome, jack@daniles.com!"

The background is run before each of your scenarios but after any of your Before Hooks. Its DRY strategy for writing cucumber scenarios. A set of "prerequisite" steps that should be executed before running actual test case.

For eg. Before checking a Dashboard or profile page contents a user must be logged in. So, logged in coverage will be done inside "Background" section Then you will have 2 test cases

Background: // Get user logged-in

Scenario : //Visit Dashboard Page .....

Scenario: //Visit Profile Page ....

For more info https://github.com/cucumber/cucumber/wiki/Background

For different set of users you can use Scenario Outline Here is example:

https://makandracards.com/makandra/18905-how-to-not-repeat-yourself-in-cucumber-scenarios

Hope this helps!!!

The point of Background is to define a common starting point for several scenarios. If you only have one scenario it only makes sense as a means of dividing things into parts. If you are familiar with unit test frameworks it is similar to the setup method in the xunit family, or a before block if you use rspec. For the purpose of running the test the stuff in background could just as well be inside the scenario. It is a mechanism for reuse.

There is no direct relation between background and the test database. All the steps you run from cucumber may hit the database. The background is used to set up a good starting state in the database. Depending on your cucumber/capybara config the database will be reset between each scenario and the backgroun run again before each scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM