日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python接口測試自動化實戰(zhàn)及代碼示例:含Get、Post等方法

年初參與到一個后臺系統(tǒng)開發(fā)的項目中,里面涉及了很多接口,我做為項目組測試人員,需要對這些接口進(jìn)行測試,一開始使用 postman 工具測試,很是方便。但隨著接口數(shù)量的增加,不光要執(zhí)行手動點擊測試,而且,一旦接口參數(shù)變動,都重新更改接口參數(shù),次數(shù)多了,使得測試效率嚴(yán)重下降。

后來我將目光轉(zhuǎn)向了自動化測試,考慮到項目組對接口質(zhì)量要求很高,需要快速開發(fā)。最終選定 python 作為腳本開發(fā)語言,使用其自帶的 requests 和 urllib 模塊進(jìn)行接口請求,使用優(yōu)化后的 unittest 測試框架編寫測試接口函數(shù),測試結(jié)果選用 HTMLTestRunner 框架予以展示,并使用 python 的 ssl 模塊支持 https 協(xié)議的驗證。接下來,我詳細(xì)地介紹這些模塊,并給出各個模塊完整的測試代碼。

1、接口請求

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

 
 
 
  1. import requests
  2. def get_method(url, para, headers):
  3. try:
  4. req = requests.get(url=url, params=para, headers=headers)
  5. except Exception as e:
  6. print(e)
  7. else:
  8. if req.status_code == "200":
  9. return req
  10. else:
  11. print("Requests Failed.")
  12. if __name__=='__main__':
  13. url = "http://www.google.com"
  14. req = get_method(url=url, para=None, headers=None)
  15. print(req.status_code)
  16. print(req.text)

輸出為:

 
 
 
  1. 200
  2.  

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

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

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

 
 
 
  1. import unittest
  2. class ApiTestSample(unittest.TestCase):
  3. def setUp(self):
  4. pass
  5. def tearDown(self):
  6. pass
  7. def jiafa(self, input01, input02):
  8. result = input01 + input02
  9. return result
  10. def test_jiafa(self):
  11. testResult = self.jiafa(input01=4, input02=5)
  12. self.assertEqual(testResult, 9)
  13. if __name__=='__main__':
  14. unittest.main()

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

 
 
 
  1. Ran 1 test in 0.000s
  2. OK

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

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

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

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

 
 
 
  1. class ExtendTestCaseParams(unittest.TestCase): 
  2. #擴展 unittest.TestCase 類,使其支持自定義參數(shù)輸入
  3. def __init__(self, method_name='runTest', canshu=None):
  4. super(ExtendTestCaseParams, self).__init__(method_name)
  5. self.canshu = canshu
  6. #靜態(tài)參數(shù)化方法
  7. @staticmethod
  8. def parametrize(testcase_klass, default_name=None, canshu=None):
  9. """ Create a suite containing all tests taken from the given
  10. subclass, passing them the parameter 'canshu'
  11. """
  12. test_loader = unittest.TestLoader()
  13. testcase_names = test_loader.getTestCaseNames(testcase_klass)
  14. suite = unittest.TestSuite()
  15. if default_name != None:
  16. for casename in testcase_names:
  17. if casename == defName:
  18. suite.addTest(testcase_klass(casename, canshu=canshu))
  19. else:
  20. for casename in testcase_names:
  21. suite.addTest(testcase_klass(casename, canshu=canshu))
  22. return suite

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

 
 
 
  1. class ApiTestSample(ExtendTestCaseParams):
  2. def setUp(self):
  3. pass
  4. def tearDown(self):
  5. pass
  6. def jiafa(self, input01, input02):
  7. result = input01 + input02
  8. return result
  9. def test_jiafa(self):
  10. input_01 = self.param[0]
  11. input_02 = self.param[1]
  12. expectedResult = self.param[2]
  13. result = self.sub(input_01, input_02)
  14. print(result)
  15. self.assertEqual(result, expectedResult)
  16. if __name__=='__main__':
  17. testData = [
  18. (10, 9, 19),
  19. (12, 13, 25),
  20. (12, 10, 22),
  21. (2, 4, 6)
  22. ]
  23. suite = unittest.TestSuite()
  24. for i in testData:
  25. suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample, 'test_jiafa', canshu=i))
  26. runner = unittest.TextTestRunner()
  27. runner.run(suite)

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

 
 
 
  1. ....
  2. ## 19
  3. 25
  4. Ran 4 tests in 0.000s
  5. 22
  6. 6
  7. 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 的使用。

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

測試結(jié)果如下:

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

 
 
 
  1. 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 格式,只需將上面代碼中的

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

修改為如下代碼:

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

4、接口測試分類

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

1)單接口測試;

2)多接口測試。

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

4.1 單接口測試

 
 
 
  1. class TestApiSample(ExtendTestCaseParams):
  2. def setUp(self):
  3. pass
  4. def tearDown(self):
  5. pass
  6. def register(self, ip, name, desc):
  7. url = 'http://%s/api/v1/reg' % ip
  8. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  9. para = {"app_name": name, "description": desc}
  10. req = self.Post(url, para, headers)
  11. return req
  12. def test_register(self):
  13. for index, value in enumerate(self.param):
  14. print('Test Token {0} parameter is {1}'.format(index, value))
  15. self.ip = self.param[1]
  16. self.name = self.param[2]
  17. self.desc = self.param[3]
  18. self.expectedValue = self.param[4]
  19. req = self.grant_register(self.ip, self.name, self.desc)
  20. self.assertIn(req.status_code, self.expectedValue, msg="Test Failed.")
  21. if __name__=='__main__':
  22. import random
  23. import string
  24. ip = '172.36.17.108'
  25. testData = [
  26. (1, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
  27. (2, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
  28. (3, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)
  29. ]
  30. suite = unittest.TestSuite()
  31. for i in testData:
  32. suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample,'test_register',canshu=i))
  33. currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
  34. path = './results'
  35. if not os.path.exists(path):
  36. os.makedirs(path)
  37. report_path = path + '/' + currentTime + "_report.html"
  38. reportTitle = '接口測試報告'
  39. desc = u'接口測試報告詳情'
  40. with open(report_path, 'wd') as f:
  41. runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
  42. 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 多接口測試

 
 
 
  1. class TestApiSample(ExtendTestCaseParams):
  2. def setUp(self):
  3. pass
  4. def tearDown(self):
  5. pass
  6. def register(self, ip, name, desc):
  7. url = 'https://%s/api/v1/reg' % ip
  8. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  9. para = {"app_name": name, "description": desc}
  10. req = self.Post(url, para, headers)
  11. return req
  12. def oauth2_basic(self, ip, name, desc):
  13. apps = self.register(ip, name, desc)
  14. apps = apps.json()
  15. url = 'http://%s/api/v1/basic' % ip
  16. data = {"client_id":apps['appId'], "client_secret":apps['appKey']}
  17. headers = None
  18. req = requests.post(url, data, headers)
  19. basic = str(req.content, encoding='utf-8')
  20. return apps, basic, req
  21. def test_oauth2_basic(self):
  22. count = 0
  23. for i in self.param:
  24. count += 1
  25. self.ip = self.param[1]
  26. self.name = self.param[2]
  27. self.desc = self.param[3]
  28. self.expected = self.param[4]
  29. apps, basic, req = self.oauth2_basic(self.ip, self.name, self.desc)
  30. self.assertIn(req.status_code, self.expected, msg="Grant Failed.")
  31. if __name__=='__main__':
  32. import random
  33. import string
  34. ipAddr = '172.36.17.108'
  35. testData = [
  36. (1, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
  37. (2, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
  38. (3, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)
  39. ]
  40. suite = unittest.TestSuite()
  41. for i in testData:
  42. suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample, 'test_oauth2_basic',
  43. canshu=i))
  44. currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
  45. path = '../Results'
  46. if not os.path.exists(path):
  47. os.makedirs(path)
  48. report_path = path + '/' + currentTime + "_report.html"
  49. reportTitle = '接口測試報告'
  50. desc = u'接口測試報告詳情'
  51. with open(report_path, 'wd') as f:
  52. runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
  53. runner.run(suite)

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

5、https 協(xié)議請求

前面我們提及的接口測試,僅是關(guān)于請求 http 協(xié)議的。然而,http 協(xié)議在傳輸過程中并不安全,通過該協(xié)議傳輸內(nèi)容容易被截取,由此人們提出了 https 協(xié)議。該協(xié)議在原先的 http 協(xié)議之外,對傳輸過程中的內(nèi)容進(jìn)行了加密處理,這樣就能確保信息在傳輸過程中的安全。目前很多公司的訪問 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)過負(fù)載均衡器。而負(fù)載均衡器一般要求支持 https 協(xié)議,這個時候就需要使用 Python 中的 ssl 模塊對證書進(jìn)行校驗;

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

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

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

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

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

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

 
 
 
  1. if __name__=='__main__':
  2. from urllib import parse, request
  3. import ssl
  4. context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  5. context.check_hostname = False
  6. context.load_cert_chain(certfile=pub_key_cert_file, keyfile=pri_key_pem_file)
  7. context.verify_mode = 2
  8. context.load_verify_locations(ca_file)
  9. req = request.Request(url=url, data=para, headers=headers, method='GET')
  10. 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)實問題逼著進(jìn)步,從一開始的走捷徑使用 API集成工具來測試接口,到后來使用自動化測試腳本實現(xiàn)接口測試,再到***增加對 https協(xié)議的支持。這一路走來,帶著遇到問題解決問題地思路,我的測試技能得到很大提升。總結(jié)這幾個月的項目經(jīng)歷就一句話:遇到問題,解決問題,能力才會得到快速提升,與大家共勉。


名稱欄目:Python接口測試自動化實戰(zhàn)及代碼示例:含Get、Post等方法
URL鏈接:http://www.dlmjj.cn/article/dpdeoog.html