Code Monkey home page Code Monkey logo

bddrest's Introduction

bddrest

Toolchain to define and verify REST API in BDD.

PyPI Status Build Status Coverage Status

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!',
        url='/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',
        url_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
  url: /books/:id
  url_parameters:
    id: '1'
  verb: GET
calls:
- response:
    headers:
    - 'Content-Type: text/plain;charset=utf-8'
    status: 404 Not Found
  title: Trying invalid book id
  url_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 string which indicates the filename to dump the story, or
                 a `callable(story) -> filename` to determine the filename.
                 A file-like object is also accepted.
                 Default is `None`, meana autodumping is disabled by default.

Auto Documentation

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

:param autodoc: A string which indicates the name of documentation file, or
                 a `callable(story) -> filename` to determine the filename.
                 A file-like object is also accepted.
                 Default is `None`, meana autodoc is disabled by default.
                 Currently only markdown is supprted.

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.

### Url 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

### Url 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)"

bddrest's People

Contributors

pylover avatar mohammadsheikhian avatar shayaniox avatar eteamin avatar mrastiak avatar maadiii avatar farzaneka avatar mohadese-yousefi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.