Behave Rest API – Web tutorial links
- How to Setup Python's BEHAVE to Automate REST APIs
- Understanding Behave Feature Files and Step Implementations
- Quick Overview of HTTP Messages
- Using Python's Request Library to Make Rest API calls
- Writing Step Implementations of your API Tests
- Using Scenario Outline to Make Your Tests Data Driven
- Behave Test Hooks - The Setup() and Teardown() Equivalent
- Run Custom Implementations in Test Hooks for Specific Scenarios
The two scenarios in the Feature file below have glaring similarities so you've decided to use a test hook to initialize the selenium web driver by using
Feature: A registered user should be able to login inside the home page Scenario Outline: Unverified user logs in to the site Given an "<user_type>" user When the user logs in using his credentials below | username | password | | Sample | only | Then he should be redirected to the site home page Example: User Type | user_type | | verified | | verified | Scenario: Verified user logs in to the site but authentication service is down Given a "verified" user When the user logs in using his credentials below | username | password | | Sample | only | Then he should be propmpted with an error page
And then you realized that the second scenario is special. You need to mock an error response from the authentication service in order for the last test to pass. But then, updating the
before_scenario() to stub the response of the authentication service will cause the first two tests to fail. So how do you rectify the problem by updating the
before_scenario() so that all the test would run as intended?
Adding an additional Step on the third Scenario is a better solution! This is just an exercise to help you when the need arises and there's no elegant solution but to update the test hooks.
Use that Parameter!
The Documentation from Behave did not explicitly show a working example but they mentioned attributes that can be accessed on different test hooks. These attributes can be accessed via second parameter that you've defined in
environment.py. Take a look at the test hooks that are available right now in Behave
before_all(context) after_all(context) before_feature(context, feature) after_feature(context, feature) before_tag(context, tag) after_tag(context, tag) before_scenario(context, scenario) after_scenario(context, scenario) before_step(context, step) after_step(context, step)
So to use the attributes you just need to add some logic on the hook and access it like a property. So to answer the problem that I've presented, here's the snippet on how to pin point what special handling you want to implement on a specific scenario.
def before_scenario(context, scenario): if scenario.name == 'Verified user logs in to the site but authentication service is down': #prepare your stub here else: #do your common procedures here
There are currently five attributes that you can use right now.
name tags keyword filename line
From the five above, I think that the ones that you'll be using most of the time will be
name which returns a string and
tags which returns a list of tags.