2012-05-29 45 views
10

我是REST和測試部門的新手。我需要編寫自動化腳本來測試我們的REST服務。我們計劃定期從Jenkins CI工作中運行這些腳本。我更喜歡用python編寫這些代碼,因爲我們已經在由selenium IDE生成的python中使用了UI功能測試腳本,但我願意接受任何優秀的解決方案。我檢查了httplib,simplejson和Xunit,但希望找到更好的解決方案。 而且,我更願意編寫一個模板,並通過從xml或其他東西讀取api信息來爲每個REST API生成實際腳本。提前感謝所有建議。REST服務自動化測試需要的建議

+0

究竟是你想測試?答案是你期望的? – Swift

+0

是的,需要驗證回覆數據。想測試所有的CRUD rest api動作。例如,使用REST API,創建五名員工,讀回員工,更新一些,最後刪除所有......我正在考慮這一行動。 – user1366786

+0

下面的Via Groovy是鏈接。 http://stackoverflow.com/questions/38972221/api-automation-groovy-soapui-all-together-for-most/38974183#38974183 –

回答

18

我通常使用Cucumber來測試我寧靜的API。以下示例在Ruby中,但可以使用the rubypy gemlettuce輕鬆轉換爲python。

開始了一組基於REST的基礎步驟:

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

感謝斯威夫特。我會效法你的榜樣。 – user1366786

+1

沒問題,很高興我能幫到你 – Swift

1
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)`