Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define the order of scenarios (or required scenario) with behave (python)

I'm using behave to test my little Django app.

I've already created the file user_management.feature that contains also this Scenario:

Scenario: register
 Given I can access registration form
  When I put "doctor" in "username" field
   And I put "tardisBlue" in "password" field
   And I put "[email protected]" in "email" field
   And I press the "Register" button
  Then the registration is successful
   And I am logged in

Everythig works fine.

The next feature I want to develop is in file project_management.feature:

Scenario: create a project
  Given I am logged in
  When I go to the home page
   And I click on "Create new Project" link
   And I fill the fields
    | field | text           |
    | name  | Save Gallifrey |
   And I click on "Save" button
   And I go to the home page
  Then I see the project name in the project list

Now when I execute my test, behave executes the feature files in alphabetical order, so project_management.feature is executed first.

It raise an error in the first given, because the user has not been created yet.

I've tested renamin the first file in 01_user_management.feature to make it work.

Do you know a better solution?

Is there some configuration file where I can specify the order of the feature file?

Or can I tell that a Scenario needs another Scenario tu run first?

like image 387
greenkey Avatar asked Aug 21 '14 20:08

greenkey


2 Answers

You should not make scenarios dependent on one another. It is absolutely possible to do this. I have multiple large and complex test suites with hundred of scenarios. No scenario of mine depends on another scenario having run before it.

When you have a large suite and there's a single scenario failing, it is extremely useful to be able to do:

behave -n 'failing scenario name'

This gets Behave to run only the failing scenario. Alternatively, there's the @wip tag that can do the same thing. However, if the scenario you want to test depends on another one, Behave won't automatically know that it should run the other scenario so you have a) to know the dependency and b) manually take care of selecting all scenarios that the one you really want to run depends on.

What I'd do in your situation (which is pretty much what I've done in the past) is implement a step Given I am logged in as .... I implement it with a regex so that I can use

Given I am logged in as an administrator
Given I am logged in as a regular user
Given I am logged in as a user with permissions to delete articles

The application I'm testing has its database preloaded with some test users that correspond to the cases above. (There's still a test for registering new users but that's independent from the preloaded users.) The Given I am logged in as ... step just logs the user in. It does not need to create the user.

One of the side benefits of doing this is that if you run your suite on a test service like Sauce Labs or BrowserStack and use Selenium, you can implement the Given I am logged in as ... step to save a lot of testing time. Each Selenium command in such a case requires a round-trip between your Behave test and the browser running on the test service, which can take significant time transiting through the Internet. Reducing the number of such interactions can make a huge difference in the time it takes to run a whole suite.

like image 161
Louis Avatar answered Oct 17 '22 09:10

Louis


There seems to be two ways to do this. One is that you can use Background to setup state for multiple Scenarios. The other is to call previous steps from other steps. The first solution would look something like this:

Feature: logins
  Test login functionality

  Background: login
    Given I can access registration form
    And I put "doctor" in "username" field
    And I put "tardisBlue" in "password" field
    And I put "[email protected]" in "email" field
    And I press the "Register" button

  Scenario: successful login
    Then the registration is successful
    And I am logged in

  Scenario: create a project
    When I go to the home page
    And I click on "Create new Project" link
    And I fill the fields
     | field | text           |
     | name  | Save Gallifrey |
    And I click on "Save" button
    And I go to the home page
    Then I see the project name in the project list
like image 44
Andrew Johnson Avatar answered Oct 17 '22 07:10

Andrew Johnson