Observations on API and Mashup Management

API and Mashup Blog

Test Driven Development with Web APIs

Often when automating test driven development we don't take the time to create a solid log of what is happening in each test. This is especially troublesome for tests that involve live connections to web APIs.

Development teams can use a free Apigee web API proxy to help give visibility into those tests that fail.
Here is an example of what is needed to use Apigee in a test driven development environment. In this case we use Ruby, but the concepts are applicable to any language.

 

  1. Setup a new proxy (or multiple proxies) at Apigee



  2. Setup your test initialization code to use the Apigee proxy URL
    require 'test/unit'
    require 'uri'
    require 'open-uri'
    require 'rubygems'
    require 'json'
    require 'active_support'
    require 'htmlentities'
    require 'twitter_search'
    class TwitterApiTest < Test::Unit::TestCase
      
      SEARCH_URL = 'http://twitter-growl-unit-test.apigee.com'
      TwitterSearch::Client::TWITTER_SEARCH_API_URL = "#{SEARCH_URL}/search.json"
      
      def test_twitter_search_api
        q = '@apigee'
        @search_tweets_client = TwitterSearch::Client.new 'Twitter Growl'
        @search_tweets_client.query(:q => q).each do |r|
          assert r.text.include?(q)
        end
      end
    end
  3. Run your tests
     


  4. use Apigee to better understand how your tests are using web APIs when they succeed and debug things when tests fail

 

As Apigee evolves we will provide more granular views of each request. In the meantime, happy testing.