2

I'm trying to understand how to use the background feature in cucumber. My current understanding is that it's used as something that happens before every Scenario. But I don't really understand it. Consider the following example:

Background:
    Scenario: Get user login
        Given That there exist a file for user credentials
        Then fetch the credentials.

So before I do my test I want to get the user credentials for a file. Then I want to run my test

Scenario: Test the user login function
When A user wants to log in with there stored credentials
Then Check if their username and password is valid

So, in short, this is what I would like to do. However, when writing this test, since they're different scenarios the second test will only show username and password as null. So what is the point of the background feature if I can't use the assigned values from the variables in that test?

  • Is the file fetching something related to your business logic or is it only test data loading? Meaning: Does your application load credentials from a file? – João Farias Apr 27 at 17:05
1

The background steps are executed before each and every scenario in the feature file. You cannot use the scenario keyword in the background. Your syntax has two scenario's and an empty background. In your case it should be something like this:

Background:
  Given That there exist a file for user credentials
  Then fetch the credentials.

Scenario: Test the user login function
  When A user wants to log in with there stored credentials
  Then Check if their username and password is valid

Scenario: Do something else with the same background
  # steps ...

Sharing data is not something the background keywords does, but you can share data between steps, read:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.