溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python接口測試自動化的示例代碼

發(fā)布時間:2021-09-10 13:38:32 來源:億速云 閱讀:183 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“Python接口測試自動化的示例代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python接口測試自動化的示例代碼”吧!

1、接口請求

python 特別是 python 3.x 中的 urllib 和 requests 模塊,是用來請求 url  的兩個主要模塊。這兩個模塊中,如果僅僅是支持 http 協(xié)議的 url 請求,推薦使用 requests  模塊。為什么這么說呢?因為愛因斯坦說過一句話:簡潔就是美。requests 模塊對 urllib 模塊又做了一層封裝,使用更加方便。該模塊支持 GET,  POST, PUT, DELETE 等請求方法。請求返回信息包含狀態(tài)碼和消息體,狀態(tài)碼用三位數(shù)字表示,消息體可用字符串,二進制或json  等格式表示。下面用一個例子來介紹一下 requests 模塊的使用。代碼如下:

import requests  def get_method(url, para, headers):  try:  req = requests.get(url=url, params=para, headers=headers)  except Exception as e:  print(e)  else:  if req.status_code == "200":  return req  else:  print("Requests Failed.")  if __name__=='__main__':  url = "http://www.google.com"  req = get_method(url=url, para=None, headers=None)  print(req.status_code)  print(req.text)

輸出為:

200  <!DOCTYPE html>  <!--STATUS OK--><html> <head><meta...(省略)

上述程序輸出狀態(tài)碼為 200,表明請求成功,返回消息體為網(wǎng)頁內(nèi)容。這里我僅對requests 模塊中的 get 請求方法做了封裝,其它方法(如  post,put,delete 等)的封裝類似。當讓你也可以不用封裝,直接使用 requests.methodName  來直接調(diào)用該方法。這里提醒一句,在實際的接口測試中,headers 和 data  都是有值的,要確保這些值的填寫正確,大部分請求下的請求失敗或返回結(jié)果錯誤,基本上都是由于這些值的缺失或錯誤造成的。更多關(guān)于 requests  模塊的介紹,請參考官方文檔。

2、測試框架優(yōu)化

unittest 是 python 中進行單元測試使用廣泛的框架,其與 java 中的單元測試框架junit 類似。該框架使用簡單,需要編寫以 test  開頭的函數(shù),選擇 unittest 框架運行測試函數(shù),測試結(jié)果在終端顯示。這里舉一個簡單的例子:

import unittest  class ApiTestSample(unittest.TestCase):  def setUp(self):  pass  def tearDown(self):  pass  def jiafa(self, input01, input02):  result = input01 + input02  return result  def test_jiafa(self):  testResult = self.jiafa(input01=4, input02=5)  self.assertEqual(testResult, 9)  if __name__=='__main__':  unittest.main()

簡單解釋下這段代碼,首先我們創(chuàng)建一個類 ApiTestSample,這個類繼承自unittest.TestCase 類。然后在這個類中寫了 jiafa  函數(shù),它有兩個參數(shù) input01,input02,返回 input01 與 input02 相加的和。接著在 test_jiafa 方法中,我們對剛才  jiafa 函數(shù)進行了和值校驗。通過給 jiafa 輸入兩個值,獲取其函數(shù)返回值,并與真實值做相等判斷,以此實現(xiàn)函數(shù)單元測試。這里用到了 unittest  中斷言值相等的 assertEqual(m, n)函數(shù),上述代碼運行結(jié)果如下:

Ran 1 test in 0.000s  OK

以上是 unittest  框架最基本的單元測試應(yīng)用,但是這個框架有個缺陷,就是不能自己傳入?yún)?shù)。對于接口來說,往往需要傳入很多參數(shù),并且這每個參數(shù)又有很多取值,如果不對原先的  unittest  框架做改變,不僅無法用來進行接口測試,而且一個個結(jié)合參數(shù)取值去寫測試代碼,工作量極其龐大,也沒有實現(xiàn)測試數(shù)據(jù)與腳本沒有分離。基于此,我們對該框架做出一下兩點優(yōu)化。

1)擴展 unittest.TestCase 類,支持自定義參數(shù)輸入;

2)測試數(shù)據(jù)與測試腳本分離,測試數(shù)據(jù)存儲在文件和數(shù)據(jù)庫中,以增強測試腳本復用性;

以下是對 unittest.TestCase 類的擴展,使其支持參數(shù)化把參數(shù)加進去。下面是具體的代碼實現(xiàn)過程:

class ExtendTestCaseParams(unittest.TestCase):  #擴展 unittest.TestCase 類,使其支持自定義參數(shù)輸入  def __init__(self, method_name='runTest', canshu=None):  super(ExtendTestCaseParams, self).__init__(method_name)  self.canshu = canshu  #靜態(tài)參數(shù)化方法  @staticmethod  def parametrize(testcase_klass, default_name=None, canshu=None):  """ Create a suite containing all tests taken from the given  subclass, passing them the parameter 'canshu'  """  test_loader = unittest.TestLoader()  testcase_names = test_loader.getTestCaseNames(testcase_klass)  suite = unittest.TestSuite()  if default_name != None:  for casename in testcase_names:  if casename == defName:  suite.addTest(testcase_klass(casename, canshu=canshu))  else:  for casename in testcase_names:  suite.addTest(testcase_klass(casename, canshu=canshu))  return suite

這里,canshu 就是優(yōu)化后加的自定義參數(shù),參數(shù)類型可以是元組或列表。下面使用這個參數(shù)化類來改寫之前的代碼。

class ApiTestSample(ExtendTestCaseParams):  def setUp(self):  pass  def tearDown(self):  pass  def jiafa(self, input01, input02):  result = input01 + input02  return result  def test_jiafa(self):  input_01 = self.param[0]  input_02 = self.param[1]  expectedResult = self.param[2]  result = self.sub(input_01, input_02)  print(result)  self.assertEqual(result, expectedResult)  if __name__=='__main__':  testData = [  (10, 9, 19),  (12, 13, 25),  (12, 10, 22),  (2, 4, 6)  ]  suite = unittest.TestSuite()  for i in testData:  suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample, 'test_jiafa', canshu=i))  runner = unittest.TextTestRunner()  runner.run(suite)

執(zhí)行結(jié)果如下:

....  ## 19  25  Ran 4 tests in 0.000s  22  6  OK

通過對 unittest 框架優(yōu)化,我們實現(xiàn)了 unittest  框架的參數(shù)化,這樣就可以用于接口測試了。雖然我們實現(xiàn)了參數(shù)化,但是測試結(jié)果的展示不夠直觀,這個時候需要一個可視化頁面來直接顯示測試結(jié)果。所幸的是,python  中有專門展示測試結(jié)果的框架:HTMLTestRunner。該框架可以將測試結(jié)果轉(zhuǎn)換為 HTML 頁面,并且該框架可以和unittest  框架***的結(jié)合起來。接下來我們講述一下 HTMLTestRunner 框架的使用。

3、測試結(jié)果可視化

HTMLTestRunner 框架可用來生成可視化測試報告,并能很好的與 unittest 框架結(jié)合使用,接下來我們以一段代碼來展示一下  HTMLTestRunner 的使用。

if __name__=='__main__':  from HTMLTestRunner import HTMLTestRunner  testData = [  (10, 9, 19),  (12, 13, 25),  (12, 10, 22),  (2, 4, 6)  ]  suite = unittest.TestSuite()  for i in testData:  suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample,'test_jiafa',canshu=i))  currentTime = time.strftime("%Y-%m-%d %H_%M_%S")  result_path = './test_results'  if not os.path.exists(path):  os.makedirs(path)  report_path = result_path + '/' + currentTime + "_report.html"  reportTitle = '測試報告'  desc = u'測試報告詳情'  with open(report_path, 'wd') as f:  runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)  runner.run(suite)

測試結(jié)果如下:

下面詳細講解一下 html 報告的生成代碼:

runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)

HTMLTestRunner 中的 stream 表示輸入流,這里我們將文件描述符傳遞給 stream,title  參數(shù)表示要輸出的測試報告主題名稱,description 參數(shù)是對測試報告的描述。在使用 HTMLTestRunner 時,有幾點需要注意:

1)HTMLTestRunner 模塊非 Python 自帶庫,需要到 HTMLTestRunner 的官網(wǎng)下載

該安裝包;

2)官網(wǎng)的 HTMLTestRunner 模塊僅支持 Python 2.x 版本,如果要在 Python  3.x中,需要修改部分代碼,修改的代碼部分請自行上網(wǎng)搜索;

如果需要生成 xml 格式,只需將上面代碼中的

runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)  runner.run(suite)

修改為如下代碼:

import xmlrunner  runner = xmlrunner.XMLTestRunner(output='report')  runner.run(suite)

4、接口測試分類

前面大家對接口請求,測試框架和測試結(jié)果可視化方面有了深入的了解。有了前面的基礎(chǔ),對于接下來理解和編寫接口測試會有很大幫助。這里我們先來講解一下接口測試與單元測試的區(qū)別。單元測試只針對函數(shù)進行多組參數(shù)測試,包括正常和異常參數(shù)組合。而接口測試是針對某一接口進行多組參數(shù)測試。實際接口測試中,我們又將接口測試分為兩種:

1)單接口測試;

2)多接口測試。

對于單接口測試,只需針對單個接口測試,測試數(shù)據(jù)根據(jù)接口文檔中的參數(shù)規(guī)則來設(shè)計測試用例;對多接口測試,首先要確保接口之間調(diào)用邏輯正確,然后再根據(jù)接口文檔中的參數(shù)規(guī)則來設(shè)計用例進行測試。下面我就根據(jù)這兩種不同情況的接口測試,用實際項目代碼展示一下。

4.1 單接口測試

class TestApiSample(ExtendTestCaseParams):  def setUp(self):  pass  def tearDown(self):  pass  def register(self, ip, name, desc):  url = 'http://%s/api/v1/reg' % ip  headers = {"Content-Type": "application/x-www-form-urlencoded"}  para = {"app_name": name, "description": desc}  req = self.Post(url, para, headers)  return req  def test_register(self):  for index, value in enumerate(self.param):  print('Test Token {0} parameter is {1}'.format(index, value))  self.ip = self.param[1]  self.name = self.param[2]  self.desc = self.param[3]  self.expectedValue = self.param[4]  req = self.grant_register(self.ip, self.name, self.desc)  self.assertIn(req.status_code, self.expectedValue, msg="Test Failed.")  if __name__=='__main__':  import random  import string  ip = '172.36.17.108'  testData = [  (1, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),  (2, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),  (3, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)  ]  suite = unittest.TestSuite()  for i in testData:  suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample,'test_register',canshu=i))  currentTime = time.strftime("%Y-%m-%d %H_%M_%S")  path = './results'  if not os.path.exists(path):  os.makedirs(path)  report_path = path + '/' + currentTime + "_report.html"  reportTitle = '接口測試報告'  desc = u'接口測試報告詳情'  with open(report_path, 'wd') as f:  runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)  runner.run(suite)

上述代碼中的 register()為注冊接口函數(shù),test_register()為測試注冊接口函數(shù),testData  為測試數(shù)據(jù),這里沒有完全做到測試腳本與測試數(shù)據(jù)分離。為了實現(xiàn)測試數(shù)據(jù)與測試腳本分離,可以將 testData  列表單獨寫在文本文件或者數(shù)據(jù)庫中,運行測試腳本時再去加載這些數(shù)據(jù),就能實現(xiàn)測試腳本與測試數(shù)據(jù)的分離。

4.2 多接口測試

class TestApiSample(ExtendTestCaseParams):  def setUp(self):  pass  def tearDown(self):  pass  def register(self, ip, name, desc):  url = 'https://%s/api/v1/reg' % ip  headers = {"Content-Type": "application/x-www-form-urlencoded"}  para = {"app_name": name, "description": desc}  req = self.Post(url, para, headers)  return req  def oauth3_basic(self, ip, name, desc):  apps = self.register(ip, name, desc)  apps = apps.json()  url = 'http://%s/api/v1/basic' % ip  data = {"client_id":apps['appId'], "client_secret":apps['appKey']}  headers = None  req = requests.post(url, data, headers)  basic = str(req.content, encoding='utf-8')  return apps, basic, req  def test_oauth3_basic(self):  count = 0  for i in self.param:  count += 1  self.ip = self.param[1]  self.name = self.param[2]  self.desc = self.param[3]  self.expected = self.param[4]  apps, basic, req = self.oauth3_basic(self.ip, self.name, self.desc)  self.assertIn(req.status_code, self.expected, msg="Grant Failed.")  if __name__=='__main__':  import random  import string  ipAddr = '172.36.17.108'  testData = [  (1, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),  (2, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),  (3, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)  ]  suite = unittest.TestSuite()  for i in testData:  suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample, 'test_oauth3_basic',  canshu=i))  currentTime = time.strftime("%Y-%m-%d %H_%M_%S")  path = '../Results'  if not os.path.exists(path):  os.makedirs(path)  report_path = path + '/' + currentTime + "_report.html"  reportTitle = '接口測試報告'  desc = u'接口測試報告詳情'  with open(report_path, 'wd') as f:  runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)  runner.run(suite)

上述代碼中,我們對兩個接口進行了函數(shù)封裝,兩個接口之間有依賴關(guān)系,oauth3_basic()函數(shù)在請求之前必須先去請求  register()函數(shù)獲取數(shù)據(jù)。對于這種多接口測試,且接口之間存在互相調(diào)用的情況,***是在調(diào)用該接口前時,將互相之間有依賴的接口封裝進該接口中,保證接口調(diào)用邏輯一致。其次再針對該接口的其它參數(shù)設(shè)計測試用例去測試該接口。

5、https 協(xié)議請求

前面我們提及的接口測試,僅是關(guān)于請求 http 協(xié)議的。然而,http 協(xié)議在傳輸過程中并不安全,通過該協(xié)議傳輸內(nèi)容容易被截取,由此人們提出了 https  協(xié)議。該協(xié)議在原先的 http 協(xié)議之外,對傳輸過程中的內(nèi)容進行了加密處理,這樣就能確保信息在傳輸過程中的安全。目前很多公司的訪問 url 都已轉(zhuǎn)換到  https 協(xié)議。因此在接口測試中也要考慮到對 https 協(xié)議訪問的支持。目前對于 https 協(xié)議訪問的處理有以下幾種方案。

***種,對于一般網(wǎng)站訪問,無法獲得支持 https 協(xié)議的證書信息,因此只能選擇忽略 ssl 校驗;

第二種,對于外部網(wǎng)絡(luò)訪問公司內(nèi)容網(wǎng)絡(luò)和內(nèi)容來說,除了要經(jīng)過防火墻外,訪問具體業(yè)務(wù)要經(jīng)過負載均衡器。而負載均衡器一般要求支持 https  協(xié)議,這個時候就需要使用 Python 中的 ssl 模塊對證書進行校驗;

關(guān)于忽略訪問 https 協(xié)議的證書校驗,這里忽略不表。重點講解 https 協(xié)議證書的校驗。在 Python 中,提供了 ssl 模塊,用于對  https 協(xié)議證書的認證。這里以一段代碼來展示該模塊的應(yīng)用。

import ssl  cont = ssl.SSLContext(ssl.PROTOCOL_SSLv23)  cont.check_hostname = False  cont.load_cert_chain(certfile=public_key, keyfile=private_key)  cont.verify_mode = 2  cont.load_verify_locations(ca_key)

上述代碼中先生成 ssl 上下文對象 cont,接下來用這個上下文對象 cont 依次進行域名校驗、證書導入、驗證模式選擇及 CA  證書驗證。cont.checkhostname 用于域名校驗,值為 True 表示進行主機名校驗,值為 False 表示不進行主機名校驗。

cont.loadcertchain(certfile=publickey, keyfile=privatekey),certfile  表示導入公鑰證書,keyfile 表示導入私鑰證書。一般情況下,Python 支持的 certfile 證書文件后綴為.crt,keyfile  證書文件后綴為.pem。cont.verifymode 為驗證模式,值為 0 表示不做證書校驗,值為 1 表示代表可選,值為 2  表示做證書校驗。cont.loadverifylocations(ca_key)表示導入CA 證書。一般的證書校驗都要經(jīng)過上述這幾個步驟。此時 ssl  證書的基本配置已完成。接下來就需要在發(fā)送 https 請求時加入證書驗證環(huán)節(jié),示例代碼如下:

req = request.Request(url=url, data=para, headers=headers, method='GET')  response = request.urlopen(req, context=self.context)

整個完整的 ssl 證書驗證代碼如下:

if __name__=='__main__':  from urllib import parse, request  import ssl  context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)  context.check_hostname = False  context.load_cert_chain(certfile=pub_key_cert_file, keyfile=pri_key_pem_file)  context.verify_mode = 2  context.load_verify_locations(ca_file)  req = request.Request(url=url, data=para, headers=headers, method='GET')  response = request.urlopen(req, context=self.context)

上述代碼中,我們選擇了 python 中 urllib 模塊做接口請求,是因為在多次對比了reuests模塊和 urllib 對 https  證書驗證的支持之后,發(fā)現(xiàn) urllib 模塊能夠很好地支持 ssl 證書校驗。更多有關(guān) python 中 ssl 模塊的信息,請參考 ssl 官方文檔。

6、總結(jié)

回顧整個項目經(jīng)過,應(yīng)該說是是被現(xiàn)實問題逼著進步,從一開始的走捷徑使用 API集成工具來測試接口,到后來使用自動化測試腳本實現(xiàn)接口測試,再到***增加對  https協(xié)議的支持。這一路走來,帶著遇到問題解決問題地思路,我的測試技能得到很大提升??偨Y(jié)這幾個月的項目經(jīng)歷就一句話:遇到問題,解決問題,能力才會得到快速提升,與大家共勉。

到此,相信大家對“Python接口測試自動化的示例代碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI