需要建议自动化REST服务测试

use*_*786 11 python testing api rest automated-tests

我是REST和测试部门的新手.我需要编写自动化脚本来测试我们的REST服务.我们计划定期从Jenkins CI工作中运行这些脚本.我更喜欢在python中编写这些,因为我们已经在selenium IDE生成的python中测试了脚本功能,但我对任何好的解决方案都持开放态度.我检查了httplib,simplejson和Xunit,但是寻找更好的解决方案.而且,我更喜欢通过从xml或其他东西读取api信息来编写模板并为每个REST API生成实际脚本.感谢所有建议.

Swi*_*ift 18

我通常使用Cucumber来测试我的restful API.以下示例在Ruby中,但可以使用rubypy gem或者莴苣轻松转换为python .

从一组RESTful基本步骤开始:

When /^I send a GET request for "([^\"]*)"$/ do |path|
  get path
end

When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, body|
  post path, body
end

When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, body|
  put path, body
end

When /^I send a DELETE request to "([^\"]*)"$/ do |path|
  delete path
end

Then /^the response should be "([^\"]*)"$/ do |status|
  last_response.status.should == status.to_i
end

Then /^the response JSON should be:$/ do |body|
  JSON.parse(last_response.body).should == JSON.parse(body)
end
Run Code Online (Sandbox Code Playgroud)

现在我们可以编写通过实际发出请求来测试API的功能.

Feature: The users endpoints

  Scenario: Creating a user
    When I send a POST request to "/users" with the following:
      """
      { "name": "Swift", "status": "awesome" }
      """
    Then the response should be "200"

  Scenario: Listing users
    Given I send a POST request to "/users" with the following:
      """
      { "name": "Swift", "status": "awesome" }
      """
    When I send a GET request for "/users"
    Then the response should be "200"
    And the response JSON should be:
      """
      [{ "name": "Swift", "status": "awesome" }]
      """

   ... etc ...
Run Code Online (Sandbox Code Playgroud)

这些很容易在您选择的CI系统上运行.请参阅以下链接以获取参考

  • 谢谢Swift.我会按照你的例子. (2认同)