在Ruby中为REST API编写单元测试

时间:2022-10-15 20:34:31

I've written a basic REST API using sinatra.

我使用sinatra编写了一个基本的REST API。

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

有人知道为它编写测试的最佳方式吗?我想用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?

我已经用curl完成了初始测试。但我想做一些更有活力的事情。这是我的第一个API——有什么需要我测试的吗?

4 个解决方案

#1


4  

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

您可能会看一下这个方法:http://anthonyeden.com/2013/07/10/testingrest -apis-with-黄瓜和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?

尽管许多人可能会说,使用Cucumber实际上是更多的应用程序或验收测试,而不是单元测试,但它确实包含了创建HTTP头和形成HTTP请求的方法,我猜这可能是您被卡住的地方?

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)

就我个人而言,我不觉得这有什么问题,因为如果你真的要对API进行单元测试,你可能需要对API可能与之对话的任何代码单元进行模拟(例如,无论你如何持久化数据)

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.

看到我一个QA的家伙不是开发,我是完全满意使用黄瓜和测试在这一水平,但我也非常感谢当开发者单元测试,因此,您可以使用rSpec不是黄瓜,也许对“架测试”将是有用的提示你要完成什么。

#2


6  

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).

最好的办法是发表意见:就我个人而言,我喜欢简单和干净。使用minitest、Watir和REST -client等工具,您可以对REST接口进行非常简单的测试,并通过实际的浏览器测试web服务(所有主要的浏览器都支持)。

#!/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.

通过简单地执行脚本运行测试用例。Ruby有许多其他测试系统和REST客户端,它们可以以类似的方式工作。

#3


3  

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

你可以尝试使用机载,这是一个专门为此目的编写的框架:

https://github.com/*lynDev/airborne

https://github.com/*lynDev/airborne

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

您可以对一个活动的API进行测试,也可以对Sinatra、Grape、Rails应用程序进行测试。

#4


0  

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

我将使用fakeweb gem对web服务进行单元测试。

#1


4  

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

您可能会看一下这个方法:http://anthonyeden.com/2013/07/10/testingrest -apis-with-黄瓜和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?

尽管许多人可能会说,使用Cucumber实际上是更多的应用程序或验收测试,而不是单元测试,但它确实包含了创建HTTP头和形成HTTP请求的方法,我猜这可能是您被卡住的地方?

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)

就我个人而言,我不觉得这有什么问题,因为如果你真的要对API进行单元测试,你可能需要对API可能与之对话的任何代码单元进行模拟(例如,无论你如何持久化数据)

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.

看到我一个QA的家伙不是开发,我是完全满意使用黄瓜和测试在这一水平,但我也非常感谢当开发者单元测试,因此,您可以使用rSpec不是黄瓜,也许对“架测试”将是有用的提示你要完成什么。

#2


6  

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).

最好的办法是发表意见:就我个人而言,我喜欢简单和干净。使用minitest、Watir和REST -client等工具,您可以对REST接口进行非常简单的测试,并通过实际的浏览器测试web服务(所有主要的浏览器都支持)。

#!/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.

通过简单地执行脚本运行测试用例。Ruby有许多其他测试系统和REST客户端,它们可以以类似的方式工作。

#3


3  

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

你可以尝试使用机载,这是一个专门为此目的编写的框架:

https://github.com/*lynDev/airborne

https://github.com/*lynDev/airborne

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

您可以对一个活动的API进行测试,也可以对Sinatra、Grape、Rails应用程序进行测试。

#4


0  

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

我将使用fakeweb gem对web服务进行单元测试。