apiaryio/dredd — pull request #44 by ecordell (2014-02-20 22:55:33+00:00)
Title: Adds before/after hooks to Dredd

This pull request adds before/after test hooks to dredd. Everything is tested, and I did some minor refactoring to try and separate concerns a bit more.

I'll copy the documentation below for explanation:

## Writing Hooks

Dredd can be configured to use a set of hook files (specified with the `--hookfiles` flag) that define before and after hooks for requests. Hookfiles can be in javascript or coffeescript, and must import the hook methods.

Requests are identified by their `name`, which is derived from the structure of the blueprint. You can print a list of the generated names with `--names`.

**Example**

Get Names:

```sh
$ dredd single_get.md http://machines.apiary.io --names
info: Machines > Machines collection > Get Machines
```

Write a hookfile:
```coffee
{before, after} = require 'hooks'

before "Machines > Machines collection > Get Machines", (transaction) ->
console.log "before"

after "Machines > Machines collection > Get Machines", (transaction) ->
console.log "after"
```

Run tests:
```sh
$ dredd single_get.md http://machines.apiary.io --hookfiles=*_hooks.*
info: Found Hookfiles: test_hooks.coffee
info: Beginning Dredd testing...
before
pass: GET /machines duration: 2965ms
after
complete: 1 passing, 0 failing, 0 errors, 0 skipped
complete: Tests took 2977ms
```