开发者

Writing unit tests in Ruby for a REST API

I've written a basic REST API using sinatra.

Does anyone know the best way to write tests for it? I would like to do so using Ruby.

I've done my initial testing开发者_如何学运维 using curl. But I'd like to do something more robust. This is my first API - is there anything specific I should be testing?


The best way is a matter of opinion :) Personally, I like simple and clean. With tools like minitest, Watir and rest-client, you can put together a very simple test of both your REST interface as well as testing your web service through actual browsers (all major browsers are supported).

#!/usr/bin/ruby
#
# Requires that you have installed the following gem packages: 
# json, minitest, watir, watir-webdrive, rest-client
# To use Chrome, you need to install chromedriver on your path

require 'rubygems'
require 'rest-client'  
require 'json'
require 'pp'
require 'minitest/autorun'
require 'watir'
require 'watir-webdriver'

class TestReportSystem < MiniTest::Unit::TestCase
   def setup
      @browser = Watir::Browser.new :chrome # Defaults to firefox. Can do Safari and IE too.
      # Log in here.....
   end

   def teardown
      @browser.close
   end

   def test_report_lists   # For minitest, the method names need to start with test
      response = RestClient.get 'http://localhost:8080/reporter/reports/getReportList'
      assert_equal response.code,200
      parsed = JSON.parse response.to_str
      assert_equal parsed.length, 3 # There are 3 reports available on the test server
   end

   def test_on_browser
      @browser.goto 'http://localhost:8080/reporter/exampleReport/simple/genReport?month=Aug&year=2012'
      assert(@browser.text.include?('Report for Aug 2012'))
   end
end

Run the test cases by simply executing the script. There are many other testing systems and REST clients for Ruby which can be put to work in a similar way.


You might have a look at this approach http://anthonyeden.com/2013/07/10/testing-rest-apis-with-cucumber-and-rack.html

although many might say that using Cucumber is really more application or Acceptance testing and not unit testing, it does contain an approach to creating the HTTP headers and forming the http request, which I'm guessing might be where you are stuck?

Personally I don't have a problem with that since if you are truely going to unit test the API, you'd likely have to mock any units of code the api might be talking with (e.g. however you are persisting the data)

Seeing as I'm a QA guy not a dev, I'd be perfectly happy with using cucumber and testing it at that level, but I also greatly appreciate it when devs unit test, so while you might use rSpec instead of Cuke, perhaps the tip towards 'rack test' will be useful to what you are trying to accomplish.


You can try using airborne which is a framework written for just this purpose:

https://github.com/brooklynDev/airborne

You can test against either a live API, or against a Sinatra, Grape, Rails application.


I would use fakeweb gem to do unit testing with web services.


I would suggest client-api gem - it has loads of useful features specific to api automation which is easy to use and to maintain scripts.

https://github.com/prashanth-sams/client-api

Interestingly, this gem binds an api automation framework within itself. So, you don't even need a framework setup.

Key Features of client-api library:

  • Custom Header, URL, and Timeout support
  • URL query string customization
  • Datatype and key-pair value validation
  • Single key-pair response validation
  • Multi key-pair response validation
  • JSON response schema validation
  • JSON response content validation
  • JSON response size validation
  • JSON response is empty? validation
  • JSON response has specific key? validation
  • JSON response array-list sorting validation (descending, ascending)
  • Response headers validation
  • JSON template as body and schema
  • Support to store JSON responses of each tests for the current run
  • Logs support for debug
  • Custom logs remover
  • Auto-handle SSL for http(s) schemes

Example specs: https://github.com/prashanth-sams/client-api/tree/master/spec/client


Add this config snippet in the spec_helper.rb file:

ClientApi.configure do |config|
  config.base_url = 'https://reqres.in'
  config.headers = {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
  config.basic_auth = {'Username' => 'ahamilton@apigee.com', 'Password' => 'myp@ssw0rd'}
  config.json_output = {'Dirname' => './output', 'Filename' => 'test'}
  config.time_out = 10  # in secs
  config.logger = {'Dirname' => './logs', 'Filename' => 'test', 'StoreFilesCount' => 2}
end

RSpec test scenarios look like,

api = ClientApi::Api.new
it "GET request" do  
  api.get('/api/users')
  expect(api.status).to eq(200)
  expect(api.message).to eq('OK')
end

it "POST request" do
  api.post('/api/users', {"name": "prashanth sams"})
  expect(api.status).to eq(201)
end

Note: This is an active project handling issues and new features based on user requirements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜