Gabbi tests are expressed in YAML as a series of HTTP requests with their expected response:
tests: - name: retrieve root GET: / status: 200
This will trigger a
GET request to
/ on the configured Target Host. The
test will pass if the response’s status code is
tests category contains an ordered sequence of test
declarations, each describing the expected response to a given request:
||The test’s name. Must be unique within a file.||required|
||An arbitrary string describing the test.|
||A string message which if set will cause the test to be skipped with the provided message.||defaults to
||Determines whether to expect this test to fail. Note that the test will be run anyway.|
When tests are generated dynamically, the
TestCase name will
include the respective test’s
name, lowercased with spaces
_. In at least some test runners this will allow
you to select and filter on test name.
|any uppercase string||
Any such key is considered an HTTP method, with the corresponding value expressing the URL.
This is a shortcut combining
method: GET url: /index
||The HTTP request method.||defaults to
||The URL to request. This can either be a full path (e.g. “/index”) or a fully qualified URL (i.e. including host and scheme, e.g. “http://example.org/index”) — see Target Host for details.||Either this or the shortcut above is required|
||A dictionary of key-value pairs representing request header names and values. These will be added to the constructed request.|
||A dictionary of query parameters that
will be added to the
||A representation to pass as the body of
a request. Note that
||Determines whether the request uses SSL
(i.e. HTTPS). Note that the
||The expected response status code.
Multiple acceptable response codes
may be provided, separated by
||A dictionary of key-value pairs
representing expected response header
names and values. If a header’s value
is wrapped in
||A list of headers which must not be present.|
||A list of string fragments expected to be present in the response body.|
A dictionary of JSONPath rules paired
with expected matches. Using this
rule requires that the content being
sent from the server is JSON (i.e. a
content type of
If the value is wrapped in
A dictionary of two keys:
This makes it possible to poll for a resource created via an asynchronous request. Use with caution.
Note that many of these items allow substitutions.
Default values for a file’s
tests may be provided via the top-level
defaults category. These take precedence over the global defaults
response_* keys are examples of Response Handlers. Custom handlers may be
created by test authors for specific use cases. See Content Handlers for more
There are a number of magical variables that can be used to make reference to the state of a current test, the one just prior or any test prior to the current one. The variables are replaced with real values during test processing.
$ENVIRON['<environment variable>']: The name of an environment variable. Its value will replace the magical variable. If the string value of the environment variable is
"False"then the resulting value will be the corresponding boolean, not a string.
$SCHEME: The current scheme/protocol (usually
$NETLOC: The host and potentially port of the request.
Immediately Prior Test¶
$COOKIE: All the cookies set by any
Set-Cookieheaders in the prior response, including only the cookie key and value pairs and no metadata (e.g.
$URL: The URL defined in the prior request, after substitutions have been made. For backwards compatibility with earlier releases
$LAST_URLmay also be used, but if
$HISTORY(see below) is being used,
$URLmust be used.
$LOCATION: The location header returned in the prior response.
$HEADERS['<header>']: The value of any header from the prior response.
$RESPONSE['<json path>']: A JSONPath query into the prior response. See JSONPath for more on formatting.
Any Previous Test¶
$HISTORY['<test name>'].<magical variable expression>: Any variable which refers to a prior test may be used in an expression that refers to any earlier test in the same file by identifying the target test by its name in a
$HISTORYdictionary. For example, to refer to a value in a JSON object in the response of a test named
This is a very powerful feature that could lead to test that are difficult for humans to read. Take care to optimize for the maintainers that will come after you, not yourself.
Where a single-quote character,
', is shown in the variables
above you may also use a double-quote character,
", but in any
given expression the same character must be used at both ends.
All of these variables may be used in all of the following fields:
response_json_paths(on the value side of the key value pair)
response_headers(on the value side of the key value pair)
With these variables it ought to be possible to traverse an API without any explicit statements about the URLs being used. If you need a replacement on a field that is not currently supported please raise an issue or provide a patch.
As all of these features needed to be tested in the development of gabbi itself, the gabbi tests are a good source of examples on how to use the functionality. See also Example Tests for a collection of examples and the gabbi-demo tutorial.
data key has some special handing to allow for a bit more
flexibility when doing a
- If the value is not a string (that is, it is a sequence or structure)
it is treated as a data structure that will be turned into a
string by the
dumpsmethod on the relevant content handler. For example if the content-type of the body is
application/jsonthe data structure will be turned into a JSON string.
- If the value is a string that begins with
<@then the rest of the string is treated as a filepath to be loaded. The path is relative to the test directory and may not traverse up into parent directories.
- If the value is an undecorated string, that’s the value.