HTTP Testing with GABBI

Standard

I’ve come across Gabbi, a simple declarative HTTP testing framework, which uses YAML files and Python to run HTTP component testing.
I found it a bit difficult to get into, so heres a simple set of examples and use-cases.
I’ve made some simple examples which you can find on my GitHub: Checkout my Gabbi Examples

Why Choose Gabbi?

It’s Simple
Tests are Readable
It’s Extendable
It’s for ANY HTTP stack
You don’t need to know Python

Getting Started

Test Declaration

# Various tests of Google
tests:

- name: Google Issues Redirect
  desc: Test for a redirect from google.com
  method: GET
  url: http://google.com
  status: 301||302

tests:

- name: Google Redirect Resolves OK
  desc: Follow the redirect from Google verify we get a valid page
  method: GET
  redirects: True
  url: http://google.co.uk
  status: 200
  response_headers:
    Set-Cookie: /.../
    Server: gws
    Content-Type: text/html; charset=ISO-8859-1

- name: Uploads forbidden
  desc: POST some data to Google.com and see we get rejected
  method: POST
  url: http://google.com
  data: "user=me"
  status: 405
  response_headers:
    Server: gws
    Content-Type: text/html; charset=UTF-8
    Allow: GET, HEAD

Simple HTTP Tests

Fixtures

Authentication

import cookielib, urllib, urllib2, os
from gabbi import fixture
url="http://192.168.0.5/Projects/bar-manager/index.php/login"
param = {'username':'admin', 'password':'password'}

class NoRedirectHandler(urllib2.HTTPRedirectHandler):
    """ Disable redirect 
    """
    def http_error_302(self, req, fp, code, msg, headers):
        print "Redirect"
        return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
    http_error_301 = http_error_303 = http_error_307 = http_error_302

class AuthenticationFixture(fixture.GabbiFixture):

    def start_fixture(self):
        """Create some sample data
        """
        p = urllib.urlencode(param)
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(NoRedirectHandler, urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        response = urllib2.urlopen(url, p)
        cookies = {k.name: k.value for k in cj}
        os.environ['cookie'] = "ci_session=%s" % cookies['ci_session'] #c[0].split(';')[0]
        cookie = "ci_session=%s" % cookies['ci_session']
    def stop_fixture(self):
        pass

Running Tests

Tests can be run using gabbi-run, called per test script (per yaml file) or using an automated test loader. Fixtures aren’t supported through gabbi-run.

gabbi-run: Useful for initial prototyping and development gabbi-run [host[:port]] < /my/test.yaml loader.py: Full automation and permits the use of fixtures

Automating Tests

I’ve used Gabbi to develop automated tests for my MyPOS Web Application, developed with CodeIgniter/PHP
Gabbi Tests can be run

Links