Flask Should-DSL

A flask extension for testing with should-dsl


Project maintained by obmarg Hosted on GitHub Pages — Theme by mattgraham

This extension adds some basic matchers to should-dsl to allow it to be used easily along with the standard flask testing setup.

Requirements

Installing

The recommended way to instal flask is via pip:

pip install flask-should-dsl

Contributing

To contribute to flask-should-dsl:

Usage

To enable the flask-should-dsl matchers, simply import the module:

import flask.ext.should_dsl

Matchers

The matchers provided by flask-should-dsl are intended to be used with the response objects that are returned by the flask test client.

The following matchers are provided by flask-should-dsl:

have_status

This checks the status of a response object

>>> response = app.get('/ok')
>>> response |should| have_status(200)
>>> response |should| have_status(400)
Traceback (most recent call last):
...
ShouldNotSatisfied: Expected the status code 400, but got 200
be_xxx / abort_xxx / return_xxx

These matchers (be_200, be_400, be_401, be_403, be_404, be_405, be_500) provide shortcuts to check the status of a response object. The matchers are avaliable with be, abort and return prefixes, to allow for more readable code depending on the circumstances

>>> response = app.get('/ok')
>>> response |should| be_200
>>> response |should_not| be_200
Traceback (most recent call last):
...
ShouldNotSatisfied: Expected the status code not to be 200
>>> app.get('/') |should_not| abort_500
>>> app.get('/') |should_not| return_404
redirect_to

This matcher checks if a response contains a redirect to another page

>>> response = app.get('/redirect')
>>> response.location = 'http://localhost/redir'
>>> response |should| redirect_to('/redir')
>>> response.location = 'http://localhost/elsewhere'
>>> response |should| redirect_to('/redir')
Traceback (most recent call last):
...
ShouldNotSatisfied: Expected a redirect to "http://localhost/redir" but got "http://localhost/elsewhere"
>>> response = app.get('/ok')
>>> response |should| redirect_to('/redir')
Traceback (most recent call last):
...
ShouldNotSatisfied: Expected a redirect status, but got 200
have_json

This matcher checks if a response object contains matching JSON.

>>> response = app.get('/json')
>>> response |should| have_json({'a': 'b'})
>>> response |should| have_json({'b': 'c'})
ShouldNotSatisfied: Expected response to have json:
    {'b': 'c'}
but got:
    {u'a': u'b'}

It's also possible to pass in keyword arguments to have_json, which will be converted into a dictionary before being compared to the json.

>>> response |should| have_json(a='b')
>>> response |should| have_json(b='c')
ShouldNotSatisfied: Expected response to have json:
    {'b': 'c'}
have_content_type

This matcher checks if a response has it's content_type set to a certain value

>>> response = app.get('/ok')
>>> response |should| have_content_type('text/html')
>>> response |should| have_content_type('application/json')
ShouldNotSatisfied: Expected content type 'application/json', got 'text/html'
>>> r |should_not| have_content_type('text/html')
ShouldNotSatisfied: Expected content type to not be 'text/html'

This matcher also supports wildcard matches, and if you do not supply both a type & a subtype, then it will match on either.

>>> response = app.get('/ok')
>>> response |should| have_content_type('html')
>>> response |should| have_content_type('text')
>>> response |should| have_content_type('text/*')
>>> response |should| have_content_type('*/html')
have_header

This matcher checks if a response has a header, and optionally checks if that header is set to a certain value.

>>> response = app.get('/ok')
>>> response |should| have_header('Content-Type')
>>> response |should_not| have_header('X-BadHeader')
>>> response |should_not| have_header('X-BadHeader', 'Something')
>>> response |should_not| have_header('X-BadHeader: Something')
>>> response |should| have_header('Content-Length', '100')
ShouldNotSatisfied: Expected header 'Content-Length' to be '100' not '0'