1101_数据处理优化

用例与用例直接的依赖关系(耦合关系)

方法一:

   初始化中 处理共用的数据 充值cookie的处理方法

import unittes
from API_AUTO.http_requests import HttpRequest
class TestLogin(unittest.TestCase):
    def setUp(self):
        self.url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login'
        self.login_data = {"mobilephone": "18688773467", "pwd": "123456"}
        self.url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge'
        self.cookies=HttpRequest().http_requests(self.url_login,self.login_data,'post').cookies
    #正常登录
    def testLogin_Correct(self):
        login_data = {"mobilephone": "18688773467", "pwd": "123456"}
        res = HttpRequest().http_requests(self.url_login,login_data,'post')
        try:
            self.assertIn('10001',res.json()['code'])
        except AssertionError as e:
            print('登录出错了%s' %e)
        return res
    def testRecharge_Correct(self):
        recharge_data = {"mobilephone": "18688773467", "amount": "1000"}
        res_recharge = HttpRequest().http_requests(self.url_recharge,recharge_data,'get',self.cookies)
        try:
            self.assertIn('10001',res_recharge.json()['code'])
        except AssertionError as e:
            print('不正常充值%s' %e)

if __name__ == '__main__':
    unittest.main()

方法二:

  使用 全局变量 处理共用的数据  (充值cookie的处理方法

import unittest
from API_AUTO.http_requests import HttpRequest
global CCOOKIE
class TestLogin(unittest.TestCase):
    def setUp(self):
        self.url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login'
        self.url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge'
    #正常登录
    def testLogin_Correct(self):
        global COOKIE #申明全局变量
        login_data = {"mobilephone": "18688773467", "pwd": "123456"}
        res = HttpRequest().http_requests(self.url_login,login_data,'post')
        if res.cookies: #如果cookie有的话,那么就更新COOKIE
            COOKIE=res.cookies
        try:
            self.assertIn('10001',res.json()['code'])
        except AssertionError as e:
            print('登录出错了%s' %e)
            raise e
    #正常充值
    def testRecharge_Correct(self):
        global COOKIE
        recharge_data = {"mobilephone": "18688773467", "amount": "1000"}
        res_recharge = HttpRequest().http_requests(self.url_recharge,recharge_data,'get',COOKIE)
        try:
            self.assertIn('10001',res_recharge.json()['code'])
        except AssertionError as e:
            print('不正常充值%s' %e)
            raise e
if __name__ == '__main__':
    unittest.main()

 

方法三:

  1)放射方法介绍:

class GetData:
    Cookie='小郭'
setattr(GetData,'Cookie','小黄') #可以直接把类里面的属性值做修改
hasattr(GetData,'Cookie') #判断是否有值
getattr(GetData,'Cookie') #获取它的值

print(GetData.Cookie)
print(hasattr(GetData,'Cookie'))
print(getattr(GetData,'Cookie'))

delattr(GetData,'Cookie') #删除这个属性
print(hasattr(GetData,'Cookie'))
View Code

   结果:

小黄
True
小黄
False

  

  2)使用 反射 处理共用的数据  (充值cookie的处理方法

import unittest
from API_AUTO.http_requests import HttpRequest
from class_1101.read_me import GetData
class TestLogin(unittest.TestCase):
    def setUp(self):
        self.url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login'
        self.url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge'
    #正常登录
    def testLogin_Correct(self):
        login_data = {"mobilephone": "18688773467", "pwd": "123456"}
        res = HttpRequest().http_requests(self.url_login,login_data,'post')
        if res.cookies: #如果cookie有的话,那么就更新COOKIE
            setattr(GetData,'Cookie',res.cookies)
        try:
            self.assertIn('10001',res.json()['code'])
        except AssertionError as e:
            print('登录出错了%s' %e)
            raise e
        return res
    #正常充值
    def testRecharge_Correct(self):
        global COOKIE
        recharge_data = {"mobilephone": "18688773467", "amount": "1000"}
        res_recharge = HttpRequest().http_requests(self.url_recharge,recharge_data,'get',getattr(GetData,'Cookie'))
        try:
            self.assertIn('10001',res_recharge.json()['code'])
        except AssertionError as e:
            print('不正常充值%s' %e)
           raise e
if __name__ == '__main__':
    unittest.main()
View Code

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

最后的结果:

class_011.py

import unittest
from API_AUTO.http_requests import HttpRequest
from class_1101.read_me import GetData

class TestLogin(unittest.TestCase):
    def setUp(self):
        pass
    def __init__(self,methodName,url,data,method,expected):
        super(TestLogin,self).__init__(methodName) #继承了父类的方法
        self.url= url
        self.data= data
        self.method=method
        self.expected=expected
    #正常登录
    def test_api(self):
        res = HttpRequest().http_requests(self.url,self.data,self.method,getattr(GetData,'Cookie'))
        if res.cookies: #如果cookie有的话,那么就更新COOKIE
            setattr(GetData,'Cookie',res.cookies)
        try:
            self.assertIn(self.expected,res.json()['code'])
        except AssertionError as e:
            print('登录出错了%s' %e)
            raise e
        return res
if __name__ == '__main__':
    unittest.main()
View Code

class_022.py

 1 import unittest
 2 import HTMLTestRunner
 3 from unit1030.class_011 import TestLogin #具体到类名
 4 test_data=[{'url':'http://119.23.241.154:8080/futureloan/mvc/api/member/login',
 5            'data':{"mobilephone": "18688773467", "pwd": "123456"},'expected':'10001','method':'post'},
 6            {'url':'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge',
 7             'data':{"mobilephone": "18688773467", "amount": "1000"},'expected':'10001','method':'get'
 8 }]
 9 #存储
10 suite = unittest.TestSuite()
11 #加载
12 loader = unittest.TestLoader()
13 for item in test_data:
14     suite.addTest(TestLogin('test_api',item['url'],item['data'],item['method'],item['expected']))
15 
16 #输出
17 with open('test_report.html','wb') as file:
18     runner = HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,title='框架学习练习',tester='GYP测试')
19     runner.run(suite)
View Code

read_me.py

1 class GetData:
2     Cookie=None #存储cookie  初始值None
3 if __name__ == '__main__':

 3、操作Excel了

 1 from openpyxl import load_workbook
 2 #手动创建Excel然后,打开Excel
 3 wb=load_workbook('test.xlsx')
 4 sheet=wb['python']#传表单名 返回一个表单对象
 5 #定位单元格 行列值
 6 res=sheet.cell(1,1).value
 7 print('最大行%s'%sheet.max_row)
 8 print('最大列%s'%sheet.max_column)
 9 print(res)
10 #PS:excle中的数据出了int,就是str

 使用Excel数据驱动

http_requests.py

 1 import requests
 2 class HttpRequest:
 3     def http_requests(self,method,url,data,cookie=None):
 4         if method =='get':
 5             res = requests.get(url,data,cookies=cookie)
 6         else:
 7             res = requests.post(url,data,cookies=cookie)
 8         return res
 9 if __name__ == '__main__':
10     url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login'
11     url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge'
12     login_data = {"mobilephone": "18688773467", "pwd": "123456"}
13     recharge_data = {"mobilephone": "18688773467", "amount": "1000"}
14     #登录
15     res = HttpRequest().http_requests(url_login,login_data,'post')
16     print("登录结果是:",res.json())
17     #充值
18     recharge_res = HttpRequest().http_requests(url_recharge,recharge_data,'get',res.cookies)
19     print("充值结果是:",recharge_res.json())
View Code

gyp1101.py

 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/2 15:25
 3 #@Author:GYP测试
 4 #@File  :gyp1101.py
 5 from openpyxl import load_workbook
 6 class datedd:
 7     def __init__(self,file_name,sheet_name):
 8         self.file_name=file_name
 9         self.sheet_name=sheet_name
10     def shuju(self):
11         wb=load_workbook(self.file_name)
12         sheet=wb[self.sheet_name]
13         list1=[]
14         for j in range(1,sheet.max_row+1):
15             dct1={}
16             # re=sheet.cell(1,1).value
17             dct1['method']=sheet.cell(j,1).value
18             dct1['url']=sheet.cell(j,2).value
19             dct1['data']=sheet.cell(j,3).value
20             dct1['expected']=sheet.cell(j,4).value
21             list1.append(dct1)
22         # print(list1)
23         return list1
24 if __name__ == '__main__':
25     datedd('xg.xlsx','python1').shuju()
View Code

read_me.py

 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/1 20:11
 3 #@Author:GYP测试
 4 #@File  :read_me.py
 5 
 6 #地址  测试数据 断言 期望值
 7 
 8 class GetData:
 9     Cookie=None #存储cookie  初始值None
10 if __name__ == '__main__':
11 
12     setattr(GetData,'Cookie','小黄') #可以直接把类里面的属性值做修改
13     hasattr(GetData,'Cookie') #判断是否有值
14     getattr(GetData,'Cookie') #获取它的值
15 
16     print(GetData.Cookie)
17     print(hasattr(GetData,'Cookie'))
18     print(getattr(GetData,'Cookie'))
19 
20     delattr(GetData,'Cookie') #删除这个属性
21     print(hasattr(GetData,'Cookie'))
View Code

class_011.py

 1 import unittest
 2 from API_AUTO.http_requests import HttpRequest
 3 from class_1101.read_me import GetData
 4 
 5 class TestLogin(unittest.TestCase):
 6     def setUp(self):
 7         pass
 8 
 9     def __init__(self,methodName,url,data,method,expected):
10         super(TestLogin,self).__init__(methodName) #继承了父类的方法
11         self.url= url
12         self.data= data
13         self.method=method
14         self.expected=expected
15     #正常登录
16     def test_api(self):
17         res = HttpRequest().http_requests(self.method,self.url,self.data,getattr(GetData,'Cookie'))
18         if res.cookies: #如果cookie有的话,那么就更新COOKIE
19             setattr(GetData,'Cookie',res.cookies)
20         try:
21             self.assertIn(self.expected,res.json()['code'])
22         except AssertionError as e:
23             print('登录出错了%s' %e)
24             raise e
25         print(res.json())
26         return res
27     def tearDown(self):
28         pass
29 if __name__ == '__main__':
30     unittest.main()
View Code

class_022.py

 1 import unittest
 2 import HTMLTestRunner
 3 from unit1030.class_011 import TestLogin #具体到类名
 4 from class_1101.gyp1101 import datedd
 5 
 6 #存储
 7 suite = unittest.TestSuite()
 8 #加载
 9 loader = unittest.TestLoader()
10 res =datedd('xg.xlsx','python1').shuju()
11 for item in res:
12     suite.addTest(TestLogin('test_api',item['url'],eval(item['data']),item['method'],str(item['expected'])))
13 #输出
14 with open('test_report.html','wb') as file:
15     runner = HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,title='框架学习练习',tester='GYP测试')
16     runner.run(suite)
View Code

 另外处理数据的方法:方法二

do_excel.py 对应gyp1101.py
 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/5 10:53
 3 #@Author:GYP测试
 4 #@File  :do_excel.py
 5 from openpyxl import load_workbook
 6 class datedd:
 7     def __init__(self,file_name,sheet_name):
 8         self.file_name=file_name
 9         self.sheet_name=sheet_name
10         self.sheet_obj=load_workbook(self.file_name)[self.sheet_name]#获取表单对象
11         self.max_row=self.sheet_obj.max_row
12     def get_data(self,i,j):
13         # print(self.sheet_obj.max_row)
14         return self.sheet_obj.cell(i,j).value
15 
16 if __name__ == '__main__':
17     res =datedd('xg.xlsx','python1').get_data(1,1)
18     print(res)
View Code
test_suite_2.py 对应class_022.py
 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/5 11:11
 3 #@Author:GYP测试
 4 #@File  :test_suite_2.py
 5 import unittest
 6 import HTMLTestRunner
 7 from class_1103.class_011 import TestLogin #具体到类名
 8 from class_1103.do_excel import datedd
 9 
10 #存储
11 suite = unittest.TestSuite()
12 #加载
13 
14 t=datedd('xg.xlsx','python1')
15 
16 for i in range(1,t.max_row+1):
17     suite.addTest(TestLogin('test_api',t.get_data(i,1),t.get_data(i,2),eval(t.get_data(i,3)),str(t.get_data(i,4))))
18 #输出
19 with open('test_report.html','wb') as file:
20     runner = HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,title='框架学习练习',tester='GYP测试')
21     runner.run(suite)
View Code
do_excel.py 再次优化 个人觉得是 参数化最好的一种方法,建议使用

 1 # -*- conding:utr-8 -*-
 2 #@Time  :2018/11/2 15:25
 3 #@Author:GYP测试
 4 #@File  :gyp1101.py
 5 from openpyxl import load_workbook
 6 class datedd:
 7     def __init__(self,file_name,sheet_name):
 8         self.file_name=file_name
 9         self.sheet_name=sheet_name
10     def get_header(self):
11         wb = load_workbook(self.file_name)
12         sheet = wb[self.sheet_name]
13         header=[]
14         for i in range(1,sheet.max_column+1):
15             header.append(sheet.cell(1,i).value)
16         # print(header)
17         return header
18     def get_data(self):
19         wb=load_workbook(self.file_name)
20         sheet=wb[self.sheet_name]
21         header=self.get_header()
22         test_data=[]
23         for i in range(2,sheet.max_row+1):
24             sub_data={}
25             for j in range(1,sheet.max_column+1):
26                 sub_data[header[j-1]]=sheet.cell(i,j).value
27             test_data.append(sub_data)
28         print(test_data)
29         return test_data
30 if __name__ == '__main__':
31     datedd('xg.xlsx','python1').get_data()
32     # datedd('xg.xlsx','python1').get_header()
View Code

 

 



 








posted @ 2018-11-02 10:56  邪狂  阅读(155)  评论(0编辑  收藏  举报
柔柔弱弱