Multiple scenarios within a specification feature file?

7.2k views Asked by At

After having gotten more comfortable in Behaviour-Driven Developement using SpecFlow, I was wondering about having multiple scenarios for the same feature as follows:

Register.feature

Feature: Register a new user
    In order to use the system,
    one must register with the system 
    so that one gets authorized and may login

Scenario: Register a new user using valid credentials
    Given I am on the registration page
    When  I have entered my desired username "UserName" and password "password"
      And I have confirmed my password "password"
      And I click the register button
    Then  I shall get confirmation that I am now a registered user

Beside the fact that my scenario might have gotten a bit too fat, one must also manage to validate other scenarios within the registration process such as:

  • Input user name is too short
  • Input password is too short
  • Input password doesn't contain numbers
  • Input password doesn't match the confirm password

Just to name a few. I have read about tags using SpecFlow Feature File so that I could perhaps do as follows:

@shorterPasswordProvided
Scenario: Register a user using a password that is too short
    Given I am on the registration page
    When  I have entered my desired user name
      And I have provided a password that is too short "allo"
      And I click the Register button
    Then  I shall get an error message which mentions about the password minimum length

@noCredentialsAtAll
Scenario: Register a user using no credentials at all
    Given I am on the registration page
    When  I click on the Register button with no credentials entered
    Then  I shall get an error message that says I have to fill all required fields in

Then, using the [BeforeScenario("myTag")] should do the trick.

The hooks allows for the execution of a subset of the tests to be executed following certain rules. So, a When method could then be executed with a predefined context, that is, the hook for which it was meant to be executed, and that is mentioned through the BeforeScenario or the like attribute.

Have I understood correctly, or am I in fog here?

Am I pushing too far?

Am I missing something?

Are all the "too short password", "no credentials provided" considered different usage scenarios, or are they something else which could only fit somewhere else in the code, like the unit tests themselves?

I mean, all those scenarios belongs to the Register feature, and as such, they shall be defined in the same Register.feature SpecFlow Feature File, right?

1

There are 1 answers

3
Ben Smith On BEST ANSWER

Ok, you have a couple of questions, so I'll work through them:

Then, using the [BeforeScenario("myTag")] should do the trick.

The BeforeScenario hook attribute is used to run some code before the scenario executes. It's often used to set-up the environment for the scenario (e.g. populate the test database with pertinent data); if used for this purpose, then the use of AfterScenario can also be used to clean-up the result of BeforeScenario.

The hooks allows for the execution of a subset of the tests to be executed following certain rules. So, a When method could then be executed with a predefined context

If I understand you correctly, you want to be able to use a tag to control when a step within the scenario can be run/not-run. This is not possible with SpecFlow's hook attributes; there is a BeforeStep hook but this only enables you to execute code before the step is run, it doesn't allow the step to be ignored.

Are all the "too short password", "no credentials provided" considered different usage scenarios, or are they something else which could only fit somewhere else in the code, like the unit tests themselves?

In your example, yes these are different scenarios for your "Register a new user" feature. If you are taking a strict BDD approach to your development, then with your "outside-in inside-out" development approach you will also implement unit tests (by falling back to TDD as part of the BDD process) which will also cover the "too short password" and "no credentials provided" validation.

As for your scenario:

When  I have entered my desired username "UserName" and password "password"

Instead of using this, use:

When I enter my  username "UserName" 
And I enter my password "password"

By doing this you will be able to re-use "When I enter my password" in "Register a user using a password that is too short". This leads me onto:

And I have provided a password that is too short "allo"

There is no need to have a separate step which states the password is too short. Just re-use:

When I enter my password "allo"

For the same reason, don't use:

When I click on the Register button with no credentials entered

just reuse:

When I click on the Register button