September 12, 2019

Are you sure the buses are still listed? Interacting with APIs with Ruby + NET::HTTP + Gauge

This is Part 2 of 2 of a blog series. Care to go back to the beginning

There are many different Ruby libraries that allow you to interact with an API:

  • Net/Http: The HTTP client built into Ruby standard library. 
  • Httparty: Built on top of Net/Http by John Nunemaker, you can GET the HTTP Response HTTP Code, Response Message, and the HTTP Headers with one call. The Google Group was last active in 2017. 
  • Faraday: Also allows you to get the status, headers, and body, allowing you a bit more to customize the HTTP request
  • Rest-Client: a "simple HTTP and REST client for Ruby, inspired by the Sinatra’s microframework style of specifying actions: get, put, post, delete".
For this sample project, where we are simply getting data from the MBTA API, we will use:

  • Net/HTTP to get data from the API
  • The JSON library to parse the data
  • The test/unit library to assert that the expected values and the actual values match up
  • ThoughtWorks Gauge as the test framework. 

September 4, 2019

Are you sure the buses are still listed? Setting up data-driven API tests with Ruby + NET::HTTP + Gauge

The Massachusetts Bay Transit Authority (MBTA) operates a series of bus lines, whose information is accessible through their API, https://api-v3.mbta.com/. How could you test that a sampling of these bus lines are still listed?
  • Route 210  | Quincy Center - Fields Corner           
  • Route 212  | Quincy Center - North Quincy           
  • Route 220  | Hingham Depot - Quincy Center         
  • Route 222  | East Weymouth - Quincy Center         
  • Route 230  | Montello Commuter Rail Station - Quincy   
  • Route 236  | South Shore Plaza - Quincy Center
Sure, you could go to the MBTA website and search for the bus route at https://www.mbta.com/schedules/bus but, as we saw in our project Are You Sure The Bus Line Is Still Listed?, what is best for humans is not best when creating an automated test. 

With this project, we are going to use the built-in Ruby Library NET::HTTP to interact with the API, and Thoughtworks Gauge to set up the test framework, and we are going to make the tests data-driven, putting the information we need to verify in a table.

Related Documentation: