【Web_接口測試_Python3_接口參數構建】正則提取接口文檔的參數,自動填充到接口模板內,自動化測試案例

#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2020/2/24 and 17:29
Project:Python3
FileName:tmp_comment.py
Description:...
1.快速構造請求頭:
(.*?):(.*)  "$1":"$2",
(.*?): (.*)  # (.*)  $1: "",  # $3
(.*?): (.*),  # (.*) $1: kwargs.get($1, ""),  # $3
2.快速構建表頭註釋:(.*?)\s(.*)  "$1":$1  #$2
3.批量去除註釋:#(.*)  ""

'''
import re
comment_path = "D:\Mytest\Python3\Python3\...\sit\\testcase\\testUI\comment_txt"

# 移除代碼內部註釋信息
def remove_comment():
    '''
    描述:移除代碼內部註釋信息,去掉#後面的內容
    舉例:
    # 註釋信息
    ""
    @return: None
    '''
    with open("D:\Mytest\Python3\Python3\...\hb_config.py", "r+", encoding="utf-8") as f:
        all = f.read().splitlines()
        for i in all:
            if "#" in str(i.strip(" ")):
                i = re.sub(r'#(.*)', "", i)
            print(i)
# 構建接口請求參數
def build_request_parameters():
    '''
    描述:批量構建函數請求體body中的參數
    舉例:
        token 登錄授權碼
    "token":kwargs.get("token", "")  # 登錄授權碼
    @return: None
    '''
    with open(comment_path, "r+", encoding="gbk") as f:
        all = f.read().splitlines()
        str_all = ""
        for i in all:
            i = re.sub(r'(.*?)\s(.*)', r'"\1": kwargs.get("\1", ""),  # \2', i)
            str_all = str_all + i + "\n"
        print(str_all)
    # with open(r"D:\Mytest\Python3\Python3\...\testfile\tmp1", "w+", encoding="gbk") as ff:
    #     ff.write(str_all)
    # return str_all
# 構建函數的默認參數
def build_default_parameters():
    """
    描述:批量構建函數的默認參數
    舉例:
        "token": kwargs.get("token", ""),  # 登錄授權碼
        "userName": kwargs.get("userName", ""),  # 姓名
    token=None, userName=None
    @return: None
    """
    with open(comment_path, "r+", encoding="gbk") as f:
        all = f.read().splitlines()
        newstr = ""
        for i in all:
            if ":" in i and "[" not in i and "{" not in i and "}" not in i:
                # i = i.split(',')[0].strip().split(":")[1].strip()
                i = i.split(":")[0].strip(" ").replace('"', "")
                newstr = newstr+i+"=None, "
        print(newstr)
def buile_comment():
    import ast
    """
    描述:批量構建函數的默認參數
    舉例:
        0 - 僅鑑權
        1 - 僅登記
        2 - 鑑權並登記
    (0-僅鑑權, 1-僅登記, 2-鑑權並登記)
    @return: None
    """
    with open(comment_path, "r+", encoding="gbk") as f:
        all = f.read().splitlines()
        user_str = ""
        for i in all:
            # i = f"{i}=user_dic['{i}'], "
            i = "".join(i.split()) + ", "
            user_str = user_str + i
        user_str = "("+user_str[0:-2]+")"
        print(user_str)
# build_request_parameters() # 快速構建接口默認參數
# build_default_parameters() # 快速構建請求參數
buile_comment() # 快速註釋枚舉
#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2020/6/28 and 10:30
Project:Python3
FileName:
Description:
1.接口測試,單接口模板
'''
import requests, time, random, linecache, os, re, datetime, json, sys, ast
from sit.library.basic import Basic

# 單個業務測試類
class test():
    # 初始化靜態參數
    def __init__(self):
        self.logTime = time.strftime('%Y%m%d_%H:%M:%S', time.localtime(time.time()))
        self.curTime = time.strftime('%Y%m%d %H%M%S', time.localtime(time.time()))
        self.nowdate_8, self.nowtime_6, self.random_3 = self.curTime.split(" ")[0], self.curTime.split(" ")[1], str(random.randint(100, 999))
    # 1.test
    def test(self, *args, **kwargs):
        message = "test"
        url = "/v1/customerLogin/test"
        headers = {"Content-Type": "application/json", "User-Agent": "PostmanRuntime/7.24.0"}
        payload = {
            "body": {
                "identifier": kwargs.get("identifier", ""),  # !登錄賬號
                "password": kwargs.get("password", ""),  # !登錄密碼
            }
        }
        res = Basic().basic_runapi(message, url, headers, payload, token=kwargs.get("token", ""))
        return res
    # 2.test
    def test(self, *args, **kwargs):
        message = "test"
        url = "/v1/customerLogin/test"
        headers = {"Content-Type": "application/json", "User-Agent": "PostmanRuntime/7.24.0"}
        payload = {
            "body": {
                "identifier": kwargs.get("identifier", ""),  # !登錄賬號
                "password": kwargs.get("password", ""),  # !登錄密碼
            }
        }
        res = Basic().basic_runapi(message, url, headers, payload, token=kwargs.get("token", ""))
        return res
    # 2.test
    def test(self, *args, **kwargs):
        message = "test"
        url = "/v1/customerLogin/test"
        headers = {"Content-Type": "application/json", "User-Agent": "PostmanRuntime/7.24.0"}
        payload = {
            "body": {
                "identifier": kwargs.get("identifier", ""),  # !登錄賬號
                "password": kwargs.get("password", ""),  # !登錄密碼
            }
        }
        res = Basic().basic_runapi(message, url, headers, payload, token=kwargs.get("token", ""))
        return res
    # 4.test
    def test(self, *args, **kwargs):
        message = "test"
        url = "/v1/customerLogin/test"
        headers = {"Content-Type": "application/json", "User-Agent": "PostmanRuntime/7.24.0"}
        payload = {
            "body": {
                "identifier": kwargs.get("identifier", ""),  # !登錄賬號
                "password": kwargs.get("password", ""),  # !登錄密碼
            }
        }
        res = Basic().basic_runapi(message, url, headers, payload, token=kwargs.get("token", ""))
        return res
    # 5.原始接口測試
    def tmp_test(self):
        message = "3.2.2.25. 查詢貸款可選期限(實時接口)"
        url = "https://"
        headers = {"Content-Type": "application/json", "User-Agent": "PostmanRuntime/7.24.0"}
        payload = {}
        print(payload)
        response = requests.post(url, headers, payload)
        print(response.json())
        return response
if __name__ == "__main__":
    pass

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章