Skip to main content

A toolchain for testing REST APIs in BDD manner.

Project description

bddrest

Toolchain to define and verify REST API in BDD.

PyPI Status Build Coverage Status Downloads Downloads Downloads

Table of Contents

Quick start

Writing tests

Using Given, when, functions as you see in the example below, you can determine and assert the behaviour of your REST API.

The story, response, status objects are some proxies for currently writing story(inside the with Given( ... ): context) and it's response. and the last response after a Given and or when.

import re
import sys
import json

from bddrest.authoring import Given, when, response, status


def wsgi_application(environ, start_response):
    path = environ['PATH_INFO']
    if path.endswith('/None'):
        start_response(
            '404 Not Found',
            [('Content-Type', 'text/plain;charset=utf-8')]
        )
        yield b''
    else:
        start_response(
            '200 OK',
            [('Content-Type', 'application/json;charset=utf-8')]
        )
        result = json.dumps(dict(
            foo='bar'
        ))
        yield result.encode()


with Given(
        wsgi_application,
        title='Quickstart!',
        path='/books/id: 1',
        as_='visitor') as story:

    assert status == 200
    assert status == '200 OK'
    assert 'foo' in response.json
    assert response.json['foo'] == 'bar'

    when(
        'Trying invalid book id',
        path_parameters={'id': None}
    )

    assert response.status == 404

Dumping a Story

story.dumps()

Produces:

base_call:
  as_: visitor
  description: As a member I have to POST a book to the library.
  form:
    name: BDD Book
  query:
    a: b
  response:
    headers:
    - 'Content-Type: application/json;charset=utf-8'
    json:
      foo: bar
    status: 200 OK
  title: Posting a book
  path: /books/:id
  path_parameters:
    id: '1'
  verb: GET
calls:
- response:
    headers:
    - 'Content-Type: text/plain;charset=utf-8'
    status: 404 Not Found
  title: Trying invalid book id
  path_parameters:
    id: None

You may load the story again from this yaml with Story.loads(yaml).

There are two additional methods available to dump and load to and from a file: story.load(file) and story.dump(file)

Auto Dumping

You may pass the autodump argument of the Given function to configure the auto-dumping:

:param autodump: A file-like object to write dumped story.
                 Default is `None`, means autodump is disabled by default.

Auto Documentation

You may pass the autodoc argument of the Given function to configure the auto-documentation:

:param autodoc: A file-like object to write documentation.
                Default is `None`, meana autodoc is disabled by default.
:param autodoc_format: Either ``markdown`` or ``html``. default is
                       ``markdown``.

Markdown

You can use story.document([formatter_factory=MarkdownFormatter]) to generate documentation in arbitrary format for example: Markdown.

There is also a CLI to do this:

bddrest document < story.yml > story.md
## Posting a book

### GET /books/:id

As a member I have to POST a book to the library.

### Path Parameters

Name | Example
--- | ---
id | 1

### Query Strings

Name | Example
--- | ---
a | b

### Form

Name | Example
--- | ---
name | BDD Book

### Response: 200 OK

#### Headers

* Content-Type: application/json;charset=utf-8

#### Body

```json
{"foo": "bar"}
```

## WHEN: Trying invalid book id

### Path Parameters

Name | Example
--- | ---
id | None

### Response: 404 Not Found

#### Headers

* Content-Type: text/plain;charset=utf-8

Command Line Interface

After installing the project a command named bddrest will be available.

bddrest -h

Enabling the bash autocompletion for bddrest

Add this into your .bashrc and or $VIRTUAL_ENV/bin/postactivate.

eval "$(register-python-argcomplete bddrest)"

Project details


Release history Release notifications | RSS feed

This version

6.3.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bddrest-6.3.0.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bddrest-6.3.0-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file bddrest-6.3.0.tar.gz.

File metadata

  • Download URL: bddrest-6.3.0.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bddrest-6.3.0.tar.gz
Algorithm Hash digest
SHA256 8d674ead7212c6c307579df426a14bd0b1cb0f07038473ae8453206b06b1e88d
MD5 844d43d42072d79cf8b6f2c59a585271
BLAKE2b-256 8c7cbecf41952b154f15271d542e53b58627469826a36ea50f4ce18a09887843

See more details on using hashes here.

File details

Details for the file bddrest-6.3.0-py3-none-any.whl.

File metadata

  • Download URL: bddrest-6.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bddrest-6.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87d0e29523014e847ee6cfd1eff897e5b7163c68a7bfb24c4a63a8b61c2c9743
MD5 66b25b0f6b0937fd00ccd833f2c7bf1b
BLAKE2b-256 3068e5df31cb0b4fe2bbda0c296203b7f0d010e4a5dd4c50f49c9138b63dbcc7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page