接口测试入门

还是先上代码:

data_test.py
from openpyxl import load_workbook
class Date_test():
    @classmethod
    def Date_test_1(cls):
        """
        配置文件读取模块
        :return:
        """
        wb = load_workbook("data_test.xlsx")
        ws = wb.get_sheet_by_name("Sheet1")
        url = 'http://localhost/api'
        list1 = []
        for i in range(1, ws.max_row + 1):
            list = []
            for j in range(1, ws.max_column + 1):
                if j == 3:
                    str = url + ws.cell(i, j).value
                else:
                    str = ws.cell(i, j).value
                list.append(str)
            list1.append(list)
        wb.close()
        Dict = {}
        for i in list1:
            str = ",".join(i)
            list = []
            list = str.split(",",1)
            Dict[list[0]] = list[1]
        return Dict
    def Date_test_2(self,test_function,test_text,x):
        """
        运行结果写入模块
        :param test_function:
        :param test_text:
        :param x:
        :return:
        """
        wb = load_workbook("data_test.xlsx")
        ws = wb.get_sheet_by_name("Sheet2")
        ws.cell(x,1).value = test_function
        ws.cell(x,2).value = test_text
        wb.save("data_test.xlsx")
        wb.close()
#Http_pg.py
import requests
class HTTP_CONSOLE():
    def http_console(self,Method,url,parameter,Cookies= None):
        if Method == "POST":
            result = requests.get(url=url,params=parameter,cookies=Cookies)
        elif Method =="GET":
            result = requests.post(url=url,data=parameter,cookies=Cookies)
        else:
            print("请求方式输入错误,目前仅支持POST/GET两种请求方式!")

        return result
#test_Case.py
from data_test import *
from Http_pg import *
import json

class test_case():

    Cookie = {}

    def test_register(self):
        str = Date_test.Date_test_1()["register"]
        list = str.split(",")
        Method = list[0]
        url = list[1]
        Dict_login = {}
        Dict_login["mobilephone"] = eval(list[2])
        Dict_login["pwd"] = eval(list[3])
        result = HTTP_CONSOLE().http_console(Method=Method, url=url, parameter=Dict_login)
        Date_test().Date_test_2(test_function="register",test_text=json.loads(result.text)["msg"],x=1)

    def test_login(self):
        str = Date_test.Date_test_1()["login"]
        list = str.split(",")
        Method = list[0]
        url = list[1]
        Dict_login= {}
        Dict_login["mobilephone"] = eval(list[2])
        Dict_login["pwd"] = eval(list[3])
        result = HTTP_CONSOLE().http_console(Method=Method,url=url,parameter=Dict_login)
        self.Cookie = result.cookies
        Date_test().Date_test_2(test_function="login", test_text=json.loads(result.text)["msg"],x=2)

    def test_recharge(self):
        str = Date_test.Date_test_1()["recharge"]
        list = str.split(",")
        Method = list[0]
        url = list[1]
        Dict_login = {}
        Dict_login["mobilephone"] = eval(list[2])
        Dict_login["amount"] = eval(list[3])
        result = HTTP_CONSOLE().http_console(Method=Method, url=url, parameter=Dict_login,Cookies = self.Cookie)
        Date_test().Date_test_2(test_function="recharge", test_text=json.loads(result.text)["msg"], x=3)

    def test_withdraw(self):
        str = Date_test.Date_test_1()["withdraw"]
        list = str.split(",")
        Method = list[0]
        url = list[1]
        Dict_login = {}
        Dict_login["mobilephone"] = eval(list[2])
        Dict_login["amount"] = eval(list[3])
        result = HTTP_CONSOLE().http_console(Method=Method, url=url, parameter=Dict_login,Cookies = self.Cookie)
        Date_test().Date_test_2(test_function="withdraw", test_text=json.loads(result.text)["msg"], x=4)

if __name__ == '__main__':
    test = test_case()
    test.test_register()
    test.test_login()
    test.test_recharge()
    test.test_withdraw()

注意点:

    没有太多的注意点,有的我在前面的随笔也都写过了,这个注意下cookies的传值就行了,其他的没什么可关注的

后记:

    其实原本是打算写成unittest框架然后放出来的,实际上也写了,关键在cookies的传值上卡住了,考虑过写到xlsx的Sheet3中然后读取传值,但是很不幸测试的网址cookies不是字典而是对象,没办法写入,只能看是换个框架还是考虑别的方法了

最后附上失败的成果:

 

posted @ 2019-04-11 15:09  桂木  阅读(252)  评论(0编辑  收藏  举报