Skip to content

matez0/behave-rest-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python versions license

behave-rest-test

This is an example how to perform behavior-driven development (BDD) based on behave to specify and test a REST API.

The presented micro-framework provides gherkin patterns for a higher level description unlike other available frameworks.

Features

How to write REST API tests

The self test is an example for test implementation following the next steps.

Prepare the project layout

Implement starting the service

Depending on how often the service needs to be restarted, it can be started either from the proper given step implementation or from a before_ function in the environment.py next to the feature files.

Create feature and test data files

In the feature files, use the patterns defined by the @given and @when and @then decorators of the rest_api.py.

For each feature file, create a python file with the same name, but replace the suffix .feature with .py, for example request_response.feature and request_response.py. In this file, define the test data referenced in the feature file.

For example, in the gherkin snippet

When GET ITEM COUNT is requested
Then service responds OK with ITEM COUNT

the expressions GET ITEM COUNT and ITEM COUNT are test data references. The test data are defined in the python file:

from helpers import Request

GET_ITEM_COUNT_REQUEST = Request(
    endpoint='/item/count/',
    method='GET',
    data=''
)

ITEM_COUNT_RESPONSE = {
    "itemCount": 123,
}

Words in test data references are joined by underscores and with the appropriate suffix _REQUEST or _RESPONSE.

Self test

Create the virtual environment:

python -m venv .venv
. .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

Run the tests:

BEHAVE_RESTEST_SELF_TEST=on behave