Python+requests+unittest+excel接口自动化测试框架_python unittest excel(2)

最后

Python崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS等更加高级的领域。Python可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

👉Python所有方向的学习路线👈

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

👉Python必备开发工具👈

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

👉Python全套学习视频👈

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

👉实战案例👈

学python就与学数学一样,是不能只看书不做题的,直接看步骤和答案会让人误以为自己全都掌握了,但是碰到生题的时候还是会一筹莫展。

因此在学习python的过程中一定要记得多动手写代码,教程只需要看一两遍即可。

👉大厂面试真题👈

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

4.2 获取excel中每个列(data_config.py)

 1 #coding:utf-8
 2 class global_var:
 3     #case_id
 4     Id = '0'
 5     request_name = '1'
 6     url = '2'
 7     run = '3'
 8     request_way = '4'
 9     header = '5'
10     case_depend = '6'
11     data_depend = '7'
12     field_depend = '8'
13     data = '9'
14     expect = '10'
15     result = '11'
16 #获取caseid
17 def get_id():
18     return global_var.Id
19 
20 #获取url
21 def get_url():
22     return global_var.url
23 
24 def get_run():
25     return global_var.run
26 
27 def get_run_way():
28     return global_var.request_way
29 
30 def get_header():
31     return global_var.header
32 
33 def get_case_depend():
34     return global_var.case_depend
35 
36 def get_data_depend():
37     return global_var.data_depend
38 
39 def get_field_depend():
40     return global_var.field_depend
41 
42 def get_data():
43     return global_var.data
44 
45 def get_expect():
46     return global_var.expect
47 
48 def get_result():
49     return global_var.result
50 
51 def get_header_value():
52     return global_var.header

4.3 解决数据依赖(dependent.py )

 1 #coding:utf-8
 2 import sys
 3 import json
 4 sys.path.append('C:/Users/lxz/Desktop/InterFace_JIA')
 5 from tool.operation_excel import OperationExcel
 6 from base.runmethod import RunMethod
 7 from operation_data.get_data import GetData
 8 from jsonpath_rw import jsonpath,parse
 9 class DependdentData:
10     def __init__(self,case_id):
11         self.case_id = case_id
12         self.opera_excel = OperationExcel()
13         self.data = GetData()
14 
15     #通过case_id去获取该case_id的整行数据
16     def get_case_line_data(self):
17         rows_data = self.opera_excel.get_rows_data(self.case_id)
18         return rows_data
19 
20     #执行依赖测试,获取结果
21     def run_dependent(self):
22         run_method = RunMethod()
23         row_num  = self.opera_excel.get_row_num(self.case_id)
24         request_data = self.data.get_data_for_json(row_num)
25         #header = self.data.is_header(row_num)
26         method = self.data.get_request_method(row_num)
27         url = self.data.get_request_url(row_num)
28         res = run_method.run_main(method,url,request_data)
29         return json.loads(res)
30 
31     #根据依赖的key去获取执行依赖测试case的响应,然后返回
32     def get_data_for_key(self,row):
33         depend_data = self.data.get_depend_key(row)
34         response_data = self.run_dependent()
35         json_exe = parse(depend_data)
36         madle = json_exe.find(response_data)
37         return [math.value for math in madle][0]
38 
39 if __name__ == '__main__':
40     order = {
41         "data": {
42             "_input_charset": "utf-8", 
43             "body": "京东订单-1710141907182334", 
44             "it_b_pay": "1d", 
45             "notify_url": "http://order.imooc.com/pay/notifyalipay", 
46             "out_trade_no": "1710141907182334", 
47             "partner": "2088002966755334", 
48             "payment_type": "1", 
49             "seller_id": "yangyan01@tcl.com", 
50             "service": "mobile.securitypay.pay", 
51             "sign": "kZBV53KuiUf5HIrVLBCcBpWDg%2FnzO%2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI%2BkFZq1jOr8C554KsZ7Gk7orC9dDbQl                        pr%2BaMmdjO30JBgjqjj4mmM%2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX%2Bo4%3D", 
52             "sign_type": "RSA", 
53             "string": "_input_charset=utf-8&body=京东订单-1710141907182334&it_b_pay=1d&notify_url=http://order.imooc.com/pay/                          notifyalipay&out_trade_no=1710141907182334&partner=2088002966755334&payment_type=1&seller_id=yangyan01@                          tcl.com&service=mobile.securitypay.pay&subject=京东订单-1710141907182334&total_fee=299&sign=kZBV53KuiUf5H                          IrVLBCcBpWDg%2FnzO%2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI%2BkFZq1jOr8C554KsZ7Gk7orC9dDbQlpr%2BaMmdjO30                          JBgjqjj4mmM%2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX%2Bo4%3D&sign_type=RSA", 
54             "subject": "京东订单-1710141907182334", 
55             "total_fee": 299
56             }, 
57             "errorCode": 1000, 
58             "errorDesc": "成功", 
59             "status": 1, 
60             "timestamp": 1507979239100
61         }
62     res = "data.out_trade_no"
63     json_exe = parse(res)
64     madle = json_exe.find(order)
65     print [math.value for math in madle][0]

五、工具类包 tool

5.1 操作excel (operation_excel.py)

 1 #coding:utf-8
 2 import xlrd
 3 from xlutils.copy import copy
 4 class OperationExcel:
 5     def __init__(self,file_name=None,sheet_id=None):
 6         if file_name:
 7             self.file_name = file_name
 8             self.sheet_id = sheet_id    
 9         else:
10             self.file_name = '../dataconfig/case1.xls'
11             self.sheet_id = 0
12         self.data = self.get_data()
13 
14     #获取sheets的内容
15     def get_data(self):
16         data = xlrd.open_workbook(self.file_name)
17         tables = data.sheets()[self.sheet_id]
18         return tables
19 
20     #获取单元格的行数
21     def get_lines(self):
22         tables = self.data
23         return tables.nrows
24 
25     #获取某一个单元格的内容
26     def get_cell_value(self,row,col):
27         return self.data.cell_value(row,col)
28 
29     #写入数据
30     def write_value(self,row,col,value):
31         '''
32         写入excel数据
33         row,col,value
34         '''
35         read_data = xlrd.open_workbook(self.file_name)
36         write_data = copy(read_data)
37         sheet_data = write_data.get_sheet(0)
38         sheet_data.write(row,col,value)
39         write_data.save(self.file_name)
40 
41     #根据对应的caseid 找到对应行的内容
42     def get_rows_data(self,case_id):
43         row_num = self.get_row_num(case_id)
44         rows_data = self.get_row_values(row_num)
45         return rows_data
46 
47     #根据对应的caseid找到对应的行号
48     def get_row_num(self,case_id):
49         num = 0
50         clols_data = self.get_cols_data()
51         for col_data in clols_data:
52             if case_id in col_data:
53                 return num
54             num = num+1
55 
56 
57     #根据行号,找到该行的内容
58     def get_row_values(self,row):
59         tables = self.data
60         row_data = tables.row_values(row)
61         return row_data
62 
63     #获取某一列的内容
64     def get_cols_data(self,col_id=None):
65         if col_id != None:
66             cols = self.data.col_values(col_id)
67         else:
68             cols = self.data.col_values(0)
69         return cols
70 
71 
72 if __name__ == '__main__':
73     opers = OperationExcel()
74     print opers.get_cell_value(1,2)

5.2判断字符串包含,判断字典是否相等(common_util.py)

 1 #coding:utf-8
 2 import json
 3 class CommonUtil:
 4     def is_contain(self,str_one,str_two):
 5         '''
 6         判断一个字符串是否再另外一个字符串中
 7         str_one:查找的字符串
 8         str_two:被查找的字符串
 9         '''
10         flag = None
11         if isinstance(str_one,unicode):
12             str_one = str_one.encode('unicode-escape').decode('string_escape')
13         return cmp(str_one,str_two)
14         if str_one in str_two:
15             flag = True
16         else:
17             flag = False
18         return flag
19 
20 
21     def is_equal_dict(self,dict_one,dict_two):
22         '''
23         判断两个字典是否相等
24         '''
25         if isinstance(dict_one,str):
26             dict_one = json.loads(dict_one)
27         if isinstance(dict_two,str):
28             dict_two = json.loads(dict_two)
29         return cmp(dict_one,dict_two)

5.3 操作header(operation_herder.py)

 1 #coding:utf-8
 2 import requests
 3 import json
 4 from operation_json import OperetionJson
 5 
 6 
 7 class OperationHeader:
 8 
 9     def __init__(self,response):
10         self.response = json.loads(response)
11 
12     def get_response_url(self):
13         '''
14         获取登录返回的token的url
15         '''
16         url = self.response['data']['url'][0]
17         return url
18 
19     def get_cookie(self):
20         '''
21         获取cookie的jar文件
22         '''
23         url = self.get_response_url()+"&callback=jQuery21008240514814031887_1508666806688&_=1508666806689"
24         cookie = requests.get(url).cookies
25         return cookie
26 
27     def write_cookie(self):
28         cookie = requests.utils.dict_from_cookiejar(self.get_cookie())
29         op_json = OperetionJson()
30         op_json.write_data(cookie)
31         
32 if __name__ == '__main__':
33     
34     url = "http://www.jd.com/passport/user/login"
35     data = {
36         "username":"18513199586",
37         "password":"111111",
38         "verify":"",
39         "referer":"https://www.jd.com"
40     }
41     res = json.dumps(requests.post(url,data).json())
42     op_header = OperationHeader(res)
43     op_header.write_cookie()

5.4 操作json文件(operation_json.py)

 1 #coding:utf-8
 2 import json
 3 class OperetionJson:
 4 
 5     def __init__(self,file_path=None):
 6         if file_path  == None:
 7             self.file_path = '../dataconfig/user.json'
 8         else:
 9             self.file_path = file_path
10         self.data = self.read_data()
11 
12     #读取json文件
13     def read_data(self):
14         with open(self.file_path) as fp:
15             data = json.load(fp)
16             return data
17 
18     #根据关键字获取数据
19     def get_data(self,id):
20         print type(self.data)
21         return self.data[id]
22 
23     #写json
24     def write_data(self,data):
25         with open('../dataconfig/cookie.json','w') as fp:
26             fp.write(json.dumps(data))
27 
28 
29 
30 if __name__ == '__main__':
31     opjson = OperetionJson()
32     print opjson.get_data('shop')

5.5 操作数据库(connect_db.py)

 1 #coding:utf-8
 2 import MySQLdb.cursors
 3 import json
 4 class OperationMysql:
 5     def __init__(self):
 6         self.conn = MySQLdb.connect(
 7             host='localhost',
 8             port=3306,
 9             user='root',
10             passwd='123456',
11             db='le_study',
12             charset='utf8',
13             cursorclass=MySQLdb.cursors.DictCursor
14             )
15         self.cur = self.conn.cursor()
16 
17     #查询一条数据
18     def search_one(self,sql):
19         self.cur.execute(sql)
20         result = self.cur.fetchone()
21         result = json.dumps(result)
22         return result
23 
24 if __name__ == '__main__':
25     op_mysql = OperationMysql()
26     res = op_mysql.search_one("select * from web_user where Name='ailiailan'")
27     print res

5.6 发送报告邮件(send_email.py)

 1 #coding:utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 class SendEmail:
 5     global send_user
 6     global email_host
 7     global password
 8     email_host = "smtp.163.com"
 9     send_user = "jiaxiaonan666@163.com"
10     password = "jia_668"
11     def send_mail(self,user_list,sub,content):
12         user = "jiaxiaonan"+"<"+send_user+">"
13         message = MIMEText(content,_subtype='plain',_charset='utf-8')
14         message['Subject'] = sub
15         message['From'] = user
16         message['To'] = ";".join(user_list)
17         server = smtplib.SMTP()
18         server.connect(email_host)
19         server.login(send_user,password)
20         server.sendmail(user,user_list,message.as_string())
21         server.close()
22 
23     def send_main(self,pass_list,fail_list):
24         pass_num = float(len(pass_list))
25         fail_num = float(len(fail_list))
26         count_num = pass_num+fail_num
27         #90%
28         pass_result = "%.2f%%" %(pass_num/count_num*100)
29         fail_result = "%.2f%%" %(fail_num/count_num*100)


**一、Python所有方向的学习路线**

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。

![img](https://img-blog.csdnimg.cn/1d40facda2b84990b8e1743f5487d455.png)  
![img](https://img-blog.csdnimg.cn/0fc11d4a31bd431dbf124f67f1749046.png)

**二、Python必备开发工具**

工具都帮大家整理好了,安装就可直接上手!![img](https://img-blog.csdnimg.cn/ff266f529c6a46c4bc28e5f895dec647.gif#pic_center)

**三、最新Python学习笔记**

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

![img](https://img-blog.csdnimg.cn/6d414e9f494742db8bcc3fa312200539.png)

**四、Python视频合集**

观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

![img](https://img-blog.csdnimg.cn/a806d9b941c645858c61d161aec43789.png)

**五、实战案例**

纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。![img](https://img-blog.csdnimg.cn/a353983317b14d3c8856824a0d6186c1.png)

**六、面试宝典**

![在这里插入图片描述](https://img-blog.csdnimg.cn/97c454a3e5b4439b8600b50011cc8fe4.png)

![在这里插入图片描述](https://img-blog.csdnimg.cn/111f5462e7df433b981dc2430bb9ad39.png)

###### **简历模板**![在这里插入图片描述](https://img-blog.csdnimg.cn/646863996ac44da8af500c049bb72fbd.png#pic_center)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值