zoukankan      html  css  js  c++  java
  • python+request+Excel做接口自动化测试(二)

    今天整了下python用request写接口测试用例,做了个粗糙的大概的执行,具体还需找时间优化。这个采用对象化,配置以及常用函数为一个类,执行测试用例为另外的一个类。
    测试用例的撰写大概如下(还需美化):


    1、写测试用例
    这里使用pyunit,读取excel中的测试用例并执行
    # coding=utf-8
    from openpyxl import load_workbook
    import sys
    import json
    reload(sys)
    sys.setdefaultencoding('utf8')
    from requestConfig import testClass
    import unittest
    class testApi(unittest.TestCase):
    def setUp(self):
    self.test=testClass()
    print ("开始")

    def tearDown(self):
    print ("结束")

    def test0000101(self):
    test=self.test
    host=test.readSheetdata('D2')
    path=test.readSheetdata('E2')
    data=test.readSheetdata('F2')
    method=test.readSheetdata('G2')
    url=host+path
    print url
    print data
    print method
    res=test.httpGetOrPost(method,url,data)
    print res
    testCode=res['code']

    def test0000201(self):
    test=self.test
    host=test.readSheetdata('D12')
    path=test.readSheetdata('E12')
    data=test.readSheetdata('F12')
    method=test.readSheetdata('G12')
    url=host+path+test.getAfterUrl()

    res = test.httpGetOrPost(method, url, data)
    print res
    test.assertResponseCode(res['code'],'H12')

    if __name__ == "__main__":
    unittest.main()
    1、配置文件
    主要是封装用到的函数。包括http请求,读excel,url配置
    import requests
    import unittest
    import json
    from openpyxl import load_workbook
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    import csv
    reload(sys)
    sys.setdefaultencoding('utf8')
    class testClass(object):
    #封装http POST 函数,返回请求response
    def httpPost(self,keyword,url):
    # data=json.dumps(keyword)
    headers={"Content-Type":"application/json"}
    res=requests.post(url,data=keyword,headers=headers)
    responseJson=res.json()
    return responseJson

    #封装http Get函数,返回response
    def httpGet(self,keyword,url,data):
    headers={"Content-Type":"application/json"}
    res=requests.get(url,data=data,headers=headers)
    responseJson=res.json()
    return responseJson


    #封装请求函数
    def httpGetOrPost(self,method,url,data):
    # global mres
    headers = {"Content-Type": "application/json"}
    if method in "get":
    mres=requests.get(url,data=data,headers=headers)
    elif method == "post":
    # postdata = json.dumps(data)
    mres=requests.post(url,data=data,headers=headers)
    elif method in"put":
    mres=requests.put(url,data=data,headers=headers)
    elif method in "delete":
    mres=requests.delete(url,data=data,headers=headers)
    else:
    mres = requests.post(url, data=data, headers=headers)
    print("错误")
    # responseJson=mres.json()
    return mres.json()


    #读excel
    def readSheetdata(self,cell):
    wb=load_workbook(r'd:apitestcase.xlsx')
    sheet=wb.active
    value=sheet[cell]
    # print(value.value)
    return value.value

    #封装断言函数
    def assertResponseCode(self,code,cell):
    excelcode=self.excelCode=self.readSheetdata(cell)
    try:
    assert excelcode==code
    except AssertionError as msg:
    print "断言失败"
    return False
    else:
    return True


    def getLoginUserMess(self):
    method="post"
    url="http://apptest.buddyniu.com/api/apps/BUDDY_API_TEST/accounts/login"
    data={"password":"e10adc3949ba59abbe56e057f20f883e","clientSecret":"a123af4e331cf61c0324cd43cbc2135d","accountId":"13590404631"}
    data = json.dumps(data)
    res=self.httpPost(data,url)
    return res

    def getAccesssToken(self):
    res=self.getLoginUserMess()
    return res['data']['accessToken']

    def getUserId(self):
    res=self.getLoginUserMess()
    return res['data']['userId']

    def getRefreshToken(self):
    res=self.getLoginUserMess()
    return res['data']['refreshToken']

    def getAfterUrl(self):
    userMess=self.getLoginUserMess()
    afterUrl="?access_token ="+userMess['data']['accessToken']+"&userId ="+userMess['data']['userId']
    return afterUrl

  • 相关阅读:
    【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
    【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
    【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解
    【SpringBoot】SpringBoot热部署和配置文件自动注入实战
    IntelliJ IDEA备忘
    接口与抽象类
    泛型(11)-泛型与数组
    泛型(10)-泛型擦除与转换
    泛型(9)-泛型方法与方法重载
    泛型(8)-Java7的"菱形"语法与泛型构造器
  • 原文地址:https://www.cnblogs.com/LinxiHuang/p/9302495.html
Copyright © 2011-2022 走看看