zoukankan      html  css  js  c++  java
  • 接口自动化练手

    测试的结构如下:

     工具类,这里设计的工具类用于读取文件,包括手机,密码,token,cartid,:

     1 import csv
     2 import pandas       #导入读取csv文件的模块
     3 import numpy
     4 from builtins import type
     5 class File():
     6     def read(self):     #读取手机密码
     7         f = open('../TestCase/test.csv', 'r', encoding='utf-8')
     8         cr = pandas.read_csv(f)
     9         p = cr['手机']
    10         w = cr['密码']
    11         phone = numpy.array(p)      #把读取的字段数据转换为列表
    12         word = numpy.array(w)
    13         f.close()
    14         return phone[-1],word[-1]       #返回最后一条数据
    15     def readcookie(self):
    16         f = open('../TestCase/cookie.csv', 'r', encoding='utf-8')
    17         cr = pandas.read_csv(f)
    18         ck = cr['cookie']
    19 
    20         cook = numpy.array(ck)
    21         f.close()
    22         return cook[-1]
    23 
    24     def readcartid(self):
    25         f = open('../TestCase/cart_id.csv', 'r', encoding='utf-8')
    26         cr = pandas.read_csv(f)
    27         cartid = cr['cart_id']
    28 
    29         cart_id = numpy.array(cartid)
    30         f.close()
    31 
    32         print(cart_id[-1])
    33         return cart_id[-1]
    34 
    35     def write(self,l):
    36         f = open('../TestCase/test.csv', 'a', encoding='utf-8')
    37         w = csv.writer(f)
    38         print(l)        #测试接收到的值
    39 
    40         w.writerow(l)
    41 
    42         f.close()
    43 
    44     def writecookie(self, cook):
    45         f = open('../TestCase/cookie.csv', 'a', encoding='utf-8')
    46         w = csv.writer(f)
    47         print(cook)  # 测试接收到的值
    48 
    49         w.writerow(cook)
    50 
    51         f.close()
    52 
    53     def writecartid(self, cart_id):
    54         f = open('../TestCase/cart_id.csv', 'a', encoding='utf-8')
    55         w = csv.writer(f)
    56         print(cart_id)  # 测试接收到的值
    57 
    58         w.writerow(cart_id)
    59 
    60         f.close()
    61 
    62 # f = File()
    63 # l = ['16093252748', 'VzQBKQv6']
    64 # print(f.read())

    业务类,包括注册,登录,加入购物车,立即购买功能:

    getMobileCode.py:

     1 import string
     2 
     3 import requests
     4 import time
     5 import random
     6 class GetMobileCode():
     7     mobile = '1' + ''.join(random.choice(string.digits) for i in range(10)) #随机生成1开头手机号
     8     def cause(self):
     9         if __name__ == '__main__':
    10             print(self.mobile)
    11         self.currStr = time.time()  #时间戳
    12         return self.mobile
    13 
    14     def getcode(self):
    15         d = {'currStr': self.currStr, 'mobile':self.mobile}
    16         r = requests.post('http://106.54.81.164:8080/front/getMobileCode', data=d)#调用post方法
    17         print(r.status_code)
    18         print(r.text)

    mobileRigester.py:

     1 import random
     2 import string
     3 import requests
     4 # #
     5 class MobileRigester():
     6     def cause_passwd(self):
     7         self.passwd = ''.join(random.choice(string.digits + string.ascii_letters) for i in range(8))
     8         return self.passwd
     9     def rigester(self,mobile):
    10         self.d = {'mobileCode':'abc','mobile':mobile, 'password':self.passwd}
    11         r = requests.post('http://106.54.81.164:8080/front/mobileRegister',data=self.d)
    12         print(r.text)
    13         return mobile,self.passwd   #返回手机号,密码的元组

    loginCheck:

     1 import requests
     2 
     3 class LoginCheck():
     4 
     5     def login(self,li):
     6         d = {'username':li[0], 'password': li[1], 'captcha': 'abc'}
     7         # print(username,passwd)
     8         r = requests.post('http://106.54.81.164:8080/front/loginCheck',data=d)
     9         print(r.text)
    10         print(r.status_code)
    11         status = r.status_code
    12         r_json = r.json()
    13         c = r.cookies
    14         return c

    buyNow.py

     1 import requests
     2 
     3 
     4 class Buy():
     5         def buynow(self,token):    #,cookie
     6             d = {'goodsId':'08d03f04c22f454c86cbb869d6163f2f','count':'2','specId':'0065f44fbd0342c1a9eafc1fa1630b53','TOKEN':token}
     7             r = requests.post('http://106.54.81.164:8080/front/cart/buyNow', data=d)      #把token放到data里一起提交
     8             print(r.text)
     9             # print('1111')
    10             return r.json()
    11 
    12 # b = Buy()
    13 # b.buynow()

    addOrder.py:

     1 import requests
     2 
     3 class order():
     4     def add(self,cartid,token):  #,cookie,cartid
     5         # print(cartid)
     6         # print(token)
     7         d = {'cartIds':cartid, 'storeId': 'b785c011f9a64d2087331abc5e581f3d', 'address_options': 'f015b66a6ac645558324e49e05f5106e', 'paytype': '1', 'isPd': '0','TOKEN':token}
     8         r = requests.post('http://106.54.81.164:8080/front/cart/addOrder', data=d)
     9         print(r.text)
    10 # o = order()
    11 # o.add()

    测试类,对以上业务进行测试:

    getmobilecode.py:

    1 from ttt.Transaction.getMobileCode import GetMobileCode#导入业务类包
    2 class test_mobilecode():
    3     def test(self):
    4         g = GetMobileCode() #创建业务类对象
    5         g.cause()
    6         g.getcode()
    7         return g.cause()
    8 t = test_mobilecode()   #测试类对象
    9 mob = t.test()

    mobilerigester.py:

     1 from ttt.TestCase.getmobilecode import mob
     2 from ttt.Transaction.mobileRigester import MobileRigester
     3 from ttt.Util.tools import File
     4 
     5 class test_rigester():
     6     def test(self):
     7         t_rigester = MobileRigester()
     8 
     9         t_rigester.cause_passwd()
    10         l = list(t_rigester.rigester(mob))      #获得返回的list类型数据
    11         f = File()
    12         f.write(l)      #把手机,密码写入文件 #在当前包下调用其他类执行open操作,只会在当前包进行查找,别的包找不到,如果没有,就会创建该文件
    13         print(l)  # 测试传入的值
    14 
    15 t_r = test_rigester()
    16 t_r.test()

    loginCheck_test.py:

     1 from ttt.Transaction.loginCheck import LoginCheck
     2 from ttt.Util.tools import File
     3 
     4 
     5 class login():
     6     def login_test(self):
     7         logincheck = LoginCheck()
     8         re = File()
     9         li = re.read()
    10         cook = logincheck.login(li)     #获得cookie
    11         cookie = [cook['TOKEN']]
    12         # pickle.dump()
    13         # cookie = cook[cook.index('='):cook.index('/') + 1]      #截取cookie的int字段
    14 
    15         w = File()
    16         w.writecookie(cookie)               #调用写cookie方法写入
    17         # print(li[0],li[1])
    18         return cook
    19 l = login()
    20 l.login_test()

    buyNow.py:

     1 from builtins import type
     2 
     3 from ttt.Transaction.buyNow import Buy
     4 from ttt.Util.tools import File
     5 
     6 
     7 class buynow():
     8     def buy(self):
     9         buy = Buy()
    10         f = File()
    11         c = f.readcookie()
    12 
    13         print(c)
    14         print(type(c))
    15         cartid = buy.buynow(c)
    16         cart_id = [cartid['cartIds']]
    17         fc = File()
    18         fc.writecartid(cart_id)
    19     # status = buy.r.status_code
    20     # r_dict = buy.r.json()
    21     # r_card = r_dict['cartIds']
    22     # print(buy.r.text)
    23 b = buynow()
    24 b.buy()

    addorder_test.py:

     1 from ttt.Transaction.addOrder import order
     2 from ttt.Util.tools import File
     3 
     4 class addOrder():
     5     def add(self):
     6 
     7         f = File()
     8         cookie = f.readcookie()
     9         cartid = f.readcartid()
    10         print('cookie:',cookie)
    11         print('cartid:',cartid)
    12 
    13         aod = order()
    14         aod.add(cartid,cookie)
    15 addorder_test = addOrder()
    16 addorder_test.add()

    还有三个存数据的csv文件:

    test.csv:

     cookie.csv:

     cart_id.csv:

  • 相关阅读:
    use paramiko to connect remote server and execute command
    protect golang source code
    adjust jedi vim to python2 and python3
    install vim plugin local file offline
    add swap file if you only have 1G RAM
    datatables hyperlink in td
    django rest framework custom json format
    【JAVA基础】网络编程
    【JAVA基础】多线程
    【JAVA基础】String类的概述和使用
  • 原文地址:https://www.cnblogs.com/x991788x/p/13577654.html
Copyright © 2011-2022 走看看