查询歌曲的接口测试,但是代码重复性比较大,进行一次简单的优化
封装方法
在编写自动化脚本的时候,都要求代码简介,对重复性较多的代码进行相关的优化
我们可以看到下面的代码每个用例中都填写了不同的参数和请求相同的接口地址,我们可以通过把相同的代码进行提取出来,然后调用这个方法,完成简化代码
# 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=' 抖音不存在芒种歌曲信息中') except Exception as msg: print('错误信息%s'%msg) print('第三个用例失败') if __name__ == '__main__': unittest.main()
1、相同的代码提取出来,单独封装成一个函数
# 相同的代码提取出来,返回歌名,方便断言 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
2、通过unittest的编写用例格式,进行编写用例,调用上面的函数
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=' 抖音不存在芒种歌曲信息中') except Exception as msg: print('错误信息%s'%msg)
3、完整代码
# 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=' 抖音不存在芒种歌曲信息中') except Exception as msg: print('错误信息%s'%msg) if __name__ == '__main__': unittest.main()
通过上面的接口我们只是简单的了解下如何可以把自己的代码写的更加好看,更加的简介。(让别人看到代码就会更加的崇拜你~~~~)
我们在通过一个小例子来练习。通过请求豆瓣网的登录接口
1、豆瓣网登录接口封装完成
封装请求的url,进行对参数化,返回断言数据
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
2、不同的账号密码进行请求
3、通过断言判断用例是否成功
完整代码
# 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()