【问题标题】:Needed advice to automate REST services test自动化 REST 服务测试所需的建议
【发布时间】:2012-05-29 04:41:41
【问题描述】:

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

【问题讨论】:

  • 你到底想测试什么?反应是你所期望的吗?
  • 是的,需要验证响应数据。考虑测试所有 CRUD rest api 操作。例如使用 REST API,创建五名员工,回读员工,更新一些并最终删除所有...我正在考虑这一系列操作。
  • 下面是通过 Groovy 的链接。 stackoverflow.com/questions/38972221/…

标签: python api rest testing automated-tests


【解决方案1】:

我通常使用Cucumber 来测试我的restful API。以下示例使用 Ruby,但可以使用 the rubypy gemlettuce 轻松转换为 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

现在我们可以编写功能,通过实际发出请求来测试 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 ...

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

【讨论】:

  • 感谢斯威夫特。我会效仿你的。
  • 没问题,很高兴能帮上忙
【解决方案2】:
import openpyxl
import requests
import json
from requests.auth import HTTPBasicAuth

urlHead='https://IP_ADDRESS_HOST:PORT_NUMBER/'

rowStartAt=2
apiColumn=2
#payloadColumn=3
responseBodyColumn=12
statusCodeColumn=13

headerTypes = {'Content-Type':'application/json',
               'Accept':'application/json',
                'Authorization': '23324'
               }

wb = openpyxl.load_workbook('Excel_WORKBOOK.xlsx')

# PROCESS EACH SHEET
for sheetName in (wb.get_sheet_names()):
    print ('Sheet Name = ' + sheetName)

    flagVar = input('Enter N To avoid APIs Sheets')
    if (flagVar=='N'):
        print ('Sheet got skipped')
        continue


    #get a sheet
    sheetObj = wb.get_sheet_by_name(sheetName)

    #for each sheet iterate the API's
    for i in range(2, sheetObj.max_row+1):
        #below is API with method type
        apiFromSheet = (sheetObj.cell(row=i, column=apiColumn).value)
        if apiFromSheet is None:
            continue

        #print (i, apiFromSheet)
        #Let's split the api
        apiType = apiFromSheet.split()[0]
        method = apiFromSheet.split()[1]

        if (apiType!='GET'):
            continue

        #lets process GET API's
        absPath = urlHead + method
        print ("REQUESTED TYPE AND PATH = ", apiType, absPath)
        print('\n')


        res = requests.get(absPath, auth=HTTPBasicAuth(user, pwd),           verify=False, headers=headerTypes)

        #LET's write res body into relevant cell
        sheetObj.cell(row=i, column=responseBodyColumn).value = (res.text)
        sheetObj.cell(row=i, column=statusCodeColumn).value =  (res.status_code)
        wb.save('Excel_WORKBOOK.xlsx')



          `#exit(0)`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2017-10-25
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多