前面咱們寫了一個關於查詢歌曲的接口測試,可是代碼重複性比較大,進行一次簡單的優化html
在編寫自動化腳本的時候,都要求代碼簡介,上一篇unittest---unittest斷言中代碼重複性比較多,咱們進行一次簡單的優化(優化方法不一樣,請勿噴)json
咱們能夠看到下面的代碼每一個用例中都填寫了不一樣的參數和請求相同的接口地址,咱們能夠經過把相同的代碼進行提取出來,而後調用這個方法,完成簡化代碼api
# coding:utf-8 import unittest import requests class Music(unittest.TestCase): def test01(self): url = 'https://api.apiopen.top/searchMusic' data = { "name":"斷橋殘雪" } r = requests.post(url,data=data) b = r.json()['result'][0]['title'] a = '斷橋殘雪' self.assertEqual(a,b) print('第一個用例經過') def test02(self): url = 'https://api.apiopen.top/searchMusic' data = { "name":"說好不哭" } r = requests.post(url,data=data) a = '周杰倫' b = r.text self.assertIn(a,b) print('第二個用例經過') def test03(self): url = 'https://api.apiopen.top/searchMusic' data = { "name":"芒種" } r = requests.post(url,data=data) a = '抖音' b = r.text try: self.assertIn(a,b,msg='\n抖音不存在芒種歌曲信息中') except Exception as msg: print('錯誤信息%s'%msg) print('第三個用例失敗') if __name__ == '__main__': unittest.main()
一、相同的代碼提取出來,單獨封裝成一個函數函數
# 相同的代碼提取出來,返回歌名,方便斷言 def select(self,name): url = 'https://api.apiopen.top/searchMusic' data = { "name":name } r = requests.post(url,data=data) b = r.json()['result'][0]['title'] return b
二、經過unittest的編寫用例格式,進行編寫用例,調用上面的函數post
def test01(self): b = '斷橋殘雪' a = self.select(b) self.assertEqual(b,a) def test02(self): a = '說好不哭' b = self.select(a) self.assertEqual(a,b) def test03(self): a = '芒種' c = '抖音' b = self.select(a) try: self.assertIn(c,b,msg='\n抖音不存在芒種歌曲信息中') except Exception as msg: print('錯誤信息%s'%msg)
三、完整代碼測試
# coding:utf-8 import unittest import requests class Music(unittest.TestCase): def select(self,name): url = 'https://api.apiopen.top/searchMusic' data = { "name":name } r = requests.post(url,data=data) b = r.json()['result'][0]['title'] return b def test01(self): b = '斷橋殘雪' a = self.select(b) self.assertEqual(b,a) def test02(self): a = '說好不哭' b = self.select(a) self.assertEqual(a,b) def test03(self): a = '芒種' c = '抖音' b = self.select(a) try: self.assertIn(c,b,msg='\n抖音不存在芒種歌曲信息中') except Exception as msg: print('錯誤信息%s'%msg) if __name__ == '__main__': unittest.main()
經過上面的接口咱們只是簡單的瞭解下如何能夠把本身的代碼寫的更加好看,更加的簡介。(讓別人看到代碼就會更加的崇拜你~~~~)優化
咱們在經過一個小例子來練習。經過請求豆瓣網的登陸接口url
一、豆瓣網登陸接口封裝完成spa
封裝請求的url,進行對參數化,返回斷言數據code
def login(self,name,password): # 登陸請求地址 url = 'https://accounts.douban.com/j/mobile/login/basic' # 請求頭 headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" } # body數據 data = { 'name':name, # 帳號 "password":password, # 密碼 "remember":"false" } r = requests.post(url,headers=headers,data=data) b = r.text return b
二、不一樣的帳號密碼進行請求
三、經過斷言判斷用例是否成功
# coding:utf-8 import requests import unittest class Test_login(unittest.TestCase): def login(self,name,password): # 登陸請求地址 url = 'https://accounts.douban.com/j/mobile/login/basic' # 請求頭 headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" } # body數據 data = { 'name':name, # 帳號 "password":password, # 密碼 "remember":"false" } r = requests.post(url,headers=headers,data=data) b = r.text return b def test01(self): # 正確的帳號,密碼 b = self.login('xxxxxxx','xxxxxxx') a = '處理成功' self.assertIn(a,b) print('用例經過') def test02(self): # 錯誤的帳號,密碼 b = self.login('12345456','821006052') a = '用戶名或密碼錯誤' self.assertIn(a,b) print('用例經過') if __name__ == '__main__': unittest.main()
安靜簡單的優化了一些代碼,可能優化的不是很好,大佬們勿噴~~~