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.
- request, response
- reusing non deterministic response value
- authentication
- request by service, callback, webhook
The self test is an example for test implementation following the next steps.
-
If the feature files need to have other paths than the default, configure behave, for example. See also the documentation of behave configuration.
-
Copy the
steps
directory to where the feature files will be.
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.
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
.
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