Skip to main content
https://travis-ci.org/apiaryio/dredd-hooks-python.svg?branch=master

About

This package contains a bridge between Dredd API Testing Framework and Python environment to ease implementation of testing hooks provided by Dredd. Write Dredd hooks in Python to glue together API Blueprint with your Python project

Usage example:

import dredd_hooks as hooks

@hooks.before_all
def foo(transactions):
    for t in transactions:
        t['request']['headers']['content-type'] = 'application/json'

Download

You can see all the available versions at PyPI.

From source (tar.gz or checkout)

Unpack the archive, enter the dredd-hooks-python directory and run:

python setup.py install

Setuptools/PyPI

Alternatively it can be installed from PyPI, either manually downloading the files and installing as described above or using:

pip install dredd_hooks

Usage

  1. Create a hook file in hooks.py:

import dredd_hooks as hooks

@hooks.before("Machines > Machines collection > Get Machines")
def before_hook(transaction):
    transaction['skip'] = "true"
  1. Run it with Dredd:

    $ dredd apiary.apib localhost:3000 --language python --hookfiles ./hooks.py

API

Module dredd_hooks defines following decorators before, after, before_all, after_all, before_each, after_each, before_validation, before_each_validation. before, before_validation and after hooks are identified by transaction name.

You can combine those decorators together. So one function can be used for different hooks but be aware that some hooks have a list of all transactions as an argument and not a single transaction.

import dredd_hooks as hooks

@hooks.after_all
@hooks.before_all
@hooks.before_each
@hooks.after_each
@hooks.before_validation('Machines > Machines collection > Get Machines')
@hooks.before("Machines > Machines collection > Get Machines")
@hooks.after("Machines > Machines collection > Get Machines")
def multi_hook_function(trans):
   if isinstance(trans, list):
       print('called with list of transactions')
   else:
       if trans['name'] == 'Machines > Machines collection > Get Machines':
           trans['skip'] = 'true'

Usage is very similar to sync JS hooks API

Contributing

  1. Fork it

  2. Create your feature branch (git checkout -b my-newfeature)

  3. Commit your changes (git commit -am 'Add some feature')

  4. Push (git push origin my-new-feature)

  5. Create a new Pull Request

Testing

Don’t forget about tests, see test directory. The project uses unittest package and tox.

For integration test with Dredd interface the project uses ruby based aruba so to get it running make sure you have Ruby installed and then do:

$ bundle install

After the setup you can run the test easily with:

$ bundle exec cucumber

More details about the integration test can be found in the dredd-hooks-template repo

copyright:

Copyright (c) 2015 Apiary Czech Republic, s.r.o.

license:

MIT, see LICENSE for details.

Metadata

Release files for dredd_hooks 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dredd_hooks 0.2.0
File Size Uploaded
dredd_hooks-0.2.0.tar.gz 9.8 kB Details

Release files / dredd_hooks-0.2.0.tar.gz

Download URL dredd_hooks-0.2.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7d0527ee269d716126de912098b6d8750fcb3755232cb902e5a360f1921df780
BLAKE2b-256 checksum
How to use checksums
0cf423234e8b673f8d4eec3c9dd79fb7e420b029c7064dea6c49ba7e22242ba8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page