Python接口測試自動(dòng)化實(shí)戰(zhàn)及代碼示例:含Get、Post等方法
年初參與到一個(gè)后臺(tái)系統(tǒng)開發(fā)的項(xiàng)目中,里面涉及了很多接口,我做為項(xiàng)目組測試人員,需要對(duì)這些接口進(jìn)行測試,一開始使用 postman 工具測試,很是方便。但隨著接口數(shù)量的增加,不光要執(zhí)行手動(dòng)點(diǎn)擊測試,而且,一旦接口參數(shù)變動(dòng),都重新更改接口參數(shù),次數(shù)多了,使得測試效率嚴(yán)重下降。
后來我將目光轉(zhuǎn)向了自動(dòng)化測試,考慮到項(xiàng)目組對(duì)接口質(zhì)量要求很高,需要快速開發(fā)。最終選定 python 作為腳本開發(fā)語言,使用其自帶的 requests 和 urllib 模塊進(jìn)行接口請(qǐng)求,使用優(yōu)化后的 unittest 測試框架編寫測試接口函數(shù),測試結(jié)果選用 HTMLTestRunner 框架予以展示,并使用 python 的 ssl 模塊支持 https 協(xié)議的驗(yàn)證。接下來,我詳細(xì)地介紹這些模塊,并給出各個(gè)模塊完整的測試代碼。
1、接口請(qǐng)求
python 特別是 python 3.x 中的 urllib 和 requests 模塊,是用來請(qǐng)求 url 的兩個(gè)主要模塊。這兩個(gè)模塊中,如果僅僅是支持 http 協(xié)議的 url 請(qǐng)求,推薦使用 requests 模塊。為什么這么說呢?因?yàn)閻垡蛩固拐f過一句話:簡潔就是美。requests 模塊對(duì) urllib 模塊又做了一層封裝,使用更加方便。該模塊支持 GET, POST, PUT, DELETE 等請(qǐng)求方法。請(qǐng)求返回信息包含狀態(tài)碼和消息體,狀態(tài)碼用三位數(shù)字表示,消息體可用字符串,二進(jìn)制或json 等格式表示。下面用一個(gè)例子來介紹一下 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,表明請(qǐng)求成功,返回消息體為網(wǎng)頁內(nèi)容。這里我僅對(duì)requests 模塊中的 get 請(qǐng)求方法做了封裝,其它方法(如 post,put,delete 等)的封裝類似。當(dāng)讓你也可以不用封裝,直接使用 requests.methodName 來直接調(diào)用該方法。這里提醒一句,在實(shí)際的接口測試中,headers 和 data 都是有值的,要確保這些值的填寫正確,大部分請(qǐng)求下的請(qǐng)求失敗或返回結(jié)果錯(cuò)誤,基本上都是由于這些值的缺失或錯(cuò)誤造成的。更多關(guān)于 requests 模塊的介紹,請(qǐng)參考官方文檔。
2、測試框架優(yōu)化
unittest 是 python 中進(jìn)行單元測試使用廣泛的框架,其與 java 中的單元測試框架junit 類似。該框架使用簡單,需要編寫以 test 開頭的函數(shù),選擇 unittest 框架運(yùn)行測試函數(shù),測試結(jié)果在終端顯示。這里舉一個(gè)簡單的例子:
- 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)建一個(gè)類 ApiTestSample,這個(gè)類繼承自u(píng)nittest.TestCase 類。然后在這個(gè)類中寫了 jiafa 函數(shù),它有兩個(gè)參數(shù) input01,input02,返回 input01 與 input02 相加的和。接著在 test_jiafa 方法中,我們對(duì)剛才 jiafa 函數(shù)進(jìn)行了和值校驗(yàn)。通過給 jiafa 輸入兩個(gè)值,獲取其函數(shù)返回值,并與真實(shí)值做相等判斷,以此實(shí)現(xiàn)函數(shù)單元測試。這里用到了 unittest 中斷言值相等的 assertEqual(m, n)函數(shù),上述代碼運(yùn)行結(jié)果如下:
- Ran 1 test in 0.000s
- OK
以上是 unittest 框架最基本的單元測試應(yīng)用,但是這個(gè)框架有個(gè)缺陷,就是不能自己傳入?yún)?shù)。對(duì)于接口來說,往往需要傳入很多參數(shù),并且這每個(gè)參數(shù)又有很多取值,如果不對(duì)原先的 unittest 框架做改變,不僅無法用來進(jìn)行接口測試,而且一個(gè)個(gè)結(jié)合參數(shù)取值去寫測試代碼,工作量極其龐大,也沒有實(shí)現(xiàn)測試數(shù)據(jù)與腳本沒有分離?;诖?,我們對(duì)該框架做出一下兩點(diǎn)優(yōu)化。
1)擴(kuò)展 unittest.TestCase 類,支持自定義參數(shù)輸入;
2)測試數(shù)據(jù)與測試腳本分離,測試數(shù)據(jù)存儲(chǔ)在文件和數(shù)據(jù)庫中,以增強(qiáng)測試腳本復(fù)用性;
以下是對(duì) unittest.TestCase 類的擴(kuò)展,使其支持參數(shù)化把參數(shù)加進(jìn)去。下面是具體的代碼實(shí)現(xiàn)過程:
- class ExtendTestCaseParams(unittest.TestCase):
- #擴(kuò)展 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ù)類型可以是元組或列表。下面使用這個(gè)參數(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
通過對(duì) unittest 框架優(yōu)化,我們實(shí)現(xiàn)了 unittest 框架的參數(shù)化,這樣就可以用于接口測試了。雖然我們實(shí)現(xiàn)了參數(shù)化,但是測試結(jié)果的展示不夠直觀,這個(gè)時(shí)候需要一個(gè)可視化頁面來直接顯示測試結(jié)果。所幸的是,python 中有專門展示測試結(jié)果的框架:HTMLTestRunner。該框架可以將測試結(jié)果轉(zhuǎn)換為 HTML 頁面,并且該框架可以和unittest 框架***的結(jié)合起來。接下來我們講述一下 HTMLTestRunner 框架的使用。
3、測試結(jié)果可視化
HTMLTestRunner 框架可用來生成可視化測試報(bào)告,并能很好的與 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 = '測試報(bào)告'
- desc = u'測試報(bào)告詳情'
- with open(report_path, 'wd') as f:
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
- runner.run(suite)
測試結(jié)果如下:
下面詳細(xì)講解一下 html 報(bào)告的生成代碼:
- runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)
HTMLTestRunner 中的 stream 表示輸入流,這里我們將文件描述符傳遞給 stream,title 參數(shù)表示要輸出的測試報(bào)告主題名稱,description 參數(shù)是對(duì)測試報(bào)告的描述。在使用 HTMLTestRunner 時(shí),有幾點(diǎn)需要注意:
1)HTMLTestRunner 模塊非 Python 自帶庫,需要到 HTMLTestRunner 的官網(wǎng)下載
該安裝包;
2)官網(wǎng)的 HTMLTestRunner 模塊僅支持 Python 2.x 版本,如果要在 Python 3.x中,需要修改部分代碼,修改的代碼部分請(qǐng)自行上網(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、接口測試分類
前面大家對(duì)接口請(qǐng)求,測試框架和測試結(jié)果可視化方面有了深入的了解。有了前面的基礎(chǔ),對(duì)于接下來理解和編寫接口測試會(huì)有很大幫助。這里我們先來講解一下接口測試與單元測試的區(qū)別。單元測試只針對(duì)函數(shù)進(jìn)行多組參數(shù)測試,包括正常和異常參數(shù)組合。而接口測試是針對(duì)某一接口進(jìn)行多組參數(shù)測試。實(shí)際接口測試中,我們又將接口測試分為兩種:
1)單接口測試;
2)多接口測試。
對(duì)于單接口測試,只需針對(duì)單個(gè)接口測試,測試數(shù)據(jù)根據(jù)接口文檔中的參數(shù)規(guī)則來設(shè)計(jì)測試用例;對(duì)多接口測試,首先要確保接口之間調(diào)用邏輯正確,然后再根據(jù)接口文檔中的參數(shù)規(guī)則來設(shè)計(jì)用例進(jìn)行測試。下面我就根據(jù)這兩種不同情況的接口測試,用實(shí)際項(xiàng)目代碼展示一下。
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 = '接口測試報(bào)告'
- desc = u'接口測試報(bào)告詳情'
- with open(report_path, 'wd') as f:
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
- runner.run(suite)
上述代碼中的 register()為注冊(cè)接口函數(shù),test_register()為測試注冊(cè)接口函數(shù),testData 為測試數(shù)據(jù),這里沒有完全做到測試腳本與測試數(shù)據(jù)分離。為了實(shí)現(xiàn)測試數(shù)據(jù)與測試腳本分離,可以將 testData 列表單獨(dú)寫在文本文件或者數(shù)據(jù)庫中,運(yùn)行測試腳本時(shí)再去加載這些數(shù)據(jù),就能實(shí)現(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 oauth2_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_oauth2_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.oauth2_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_oauth2_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 = '接口測試報(bào)告'
- desc = u'接口測試報(bào)告詳情'
- with open(report_path, 'wd') as f:
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
- runner.run(suite)
上述代碼中,我們對(duì)兩個(gè)接口進(jìn)行了函數(shù)封裝,兩個(gè)接口之間有依賴關(guān)系,oauth2_basic()函數(shù)在請(qǐng)求之前必須先去請(qǐng)求 register()函數(shù)獲取數(shù)據(jù)。對(duì)于這種多接口測試,且接口之間存在互相調(diào)用的情況,***是在調(diào)用該接口前時(shí),將互相之間有依賴的接口封裝進(jìn)該接口中,保證接口調(diào)用邏輯一致。其次再針對(duì)該接口的其它參數(shù)設(shè)計(jì)測試用例去測試該接口。
5、https 協(xié)議請(qǐng)求
前面我們提及的接口測試,僅是關(guān)于請(qǐng)求 http 協(xié)議的。然而,http 協(xié)議在傳輸過程中并不安全,通過該協(xié)議傳輸內(nèi)容容易被截取,由此人們提出了 https 協(xié)議。該協(xié)議在原先的 http 協(xié)議之外,對(duì)傳輸過程中的內(nèi)容進(jìn)行了加密處理,這樣就能確保信息在傳輸過程中的安全。目前很多公司的訪問 url 都已轉(zhuǎn)換到 https 協(xié)議。因此在接口測試中也要考慮到對(duì) https 協(xié)議訪問的支持。目前對(duì)于 https 協(xié)議訪問的處理有以下幾種方案。
***種,對(duì)于一般網(wǎng)站訪問,無法獲得支持 https 協(xié)議的證書信息,因此只能選擇忽略 ssl 校驗(yàn);
第二種,對(duì)于外部網(wǎng)絡(luò)訪問公司內(nèi)容網(wǎng)絡(luò)和內(nèi)容來說,除了要經(jīng)過防火墻外,訪問具體業(yè)務(wù)要經(jīng)過負(fù)載均衡器。而負(fù)載均衡器一般要求支持 https 協(xié)議,這個(gè)時(shí)候就需要使用 Python 中的 ssl 模塊對(duì)證書進(jìn)行校驗(yàn);
關(guān)于忽略訪問 https 協(xié)議的證書校驗(yàn),這里忽略不表。重點(diǎn)講解 https 協(xié)議證書的校驗(yàn)。在 Python 中,提供了 ssl 模塊,用于對(duì) https 協(xié)議證書的認(rèn)證。這里以一段代碼來展示該模塊的應(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 上下文對(duì)象 cont,接下來用這個(gè)上下文對(duì)象 cont 依次進(jìn)行域名校驗(yàn)、證書導(dǎo)入、驗(yàn)證模式選擇及 CA 證書驗(yàn)證。cont.checkhostname 用于域名校驗(yàn),值為 True 表示進(jìn)行主機(jī)名校驗(yàn),值為 False 表示不進(jìn)行主機(jī)名校驗(yàn)。
cont.loadcertchain(certfile=publickey, keyfile=privatekey),certfile 表示導(dǎo)入公鑰證書,keyfile 表示導(dǎo)入私鑰證書。一般情況下,Python 支持的 certfile 證書文件后綴為.crt,keyfile 證書文件后綴為.pem。cont.verifymode 為驗(yàn)證模式,值為 0 表示不做證書校驗(yàn),值為 1 表示代表可選,值為 2 表示做證書校驗(yàn)。cont.loadverifylocations(ca_key)表示導(dǎo)入CA 證書。一般的證書校驗(yàn)都要經(jīng)過上述這幾個(gè)步驟。此時(shí) ssl 證書的基本配置已完成。接下來就需要在發(fā)送 https 請(qǐng)求時(shí)加入證書驗(yàn)證環(huán)節(jié),示例代碼如下:
- req = request.Request(url=url, data=para, headers=headers, method='GET')
- response = request.urlopen(req, context=self.context)
整個(gè)完整的 ssl 證書驗(yàn)證代碼如下:
- 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 模塊做接口請(qǐng)求,是因?yàn)樵诙啻螌?duì)比了reuests模塊和 urllib 對(duì) https 證書驗(yàn)證的支持之后,發(fā)現(xiàn) urllib 模塊能夠很好地支持 ssl 證書校驗(yàn)。更多有關(guān) python 中 ssl 模塊的信息,請(qǐng)參考 ssl 官方文檔。
6、總結(jié)
回顧整個(gè)項(xiàng)目經(jīng)過,應(yīng)該說是是被現(xiàn)實(shí)問題逼著進(jìn)步,從一開始的走捷徑使用 API集成工具來測試接口,到后來使用自動(dòng)化測試腳本實(shí)現(xiàn)接口測試,再到***增加對(duì) https協(xié)議的支持。這一路走來,帶著遇到問題解決問題地思路,我的測試技能得到很大提升。總結(jié)這幾個(gè)月的項(xiàng)目經(jīng)歷就一句話:遇到問題,解決問題,能力才會(huì)得到快速提升,與大家共勉。