Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Custom Formatters

aslakhellesoy edited this page Aug 13, 2010 · 41 revisions

It’s easy to write your own custom formatter for Cucumber. Maybe you want to create a formatter that posts a message to Twitter every time a step fails? (I’m sure that would get you a lot of followers). Here is how you’d do that:

Save your custom formatter class in features/support (or if you want to put it elsewhere, put a file in that directory that requires your formatter class).

# features/support/twitter_formatter.rb
require 'rubygems'
require 'twitter'

module Silly
  class TwitterFormatter
    def initialize(io, step_mother, options={})
      # We don't care about these - we're just twittering!
    end

    def scenario_executing(scenario)
      @failed = false
    end

    def scenario_executed(scenario)
      status = @failed ? 'FAILED' : 'PASSED'
      message = "#{scenario.name} #{status}"
      Twitter::Base.new('your email', 'your password').post(message)
    end

    def step_failed(step, regexp, args)
      @failed = true
    end
  end
end

Now you can run your features by passing --format Silly::TwitterFormatter to the cucumber command line, Rake task or even in cucumber.yml. You have more methods available than the ones used in this awesome example (but you only need to implement the ones you care about). Look at the sources for some of Cucumber’s built-in formatters to discover more methods.

IMPORTANT! The Formatter API will change (and hopefully stabilise) with Cucumber v0.2.

Clone this wiki locally