美文网首页
基于Python的接口自动化测试(1)

基于Python的接口自动化测试(1)

作者: 木沐__ | 来源:发表于2017-10-02 09:47 被阅读0次

    封装接口请求requests

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    __author__ = 'Jing'
    __mtime__ = 2017/09/17
    """
    import requests
    import operateDB
    from config import readConfig
    from logger import Log
    
    log = Log()
    
    class Request(object):
        def __httpGet(self,  api_url, header_data, api_param, env):
            try:
                if api_url != "":
                    req_url = env + api_url + api_param
                    response = requests.get(url=req_url, headers=header_data, timeout=10, verify=False)
    
                    if response.status_code == 200:
                        result = {"code": "00", "message": "接口Get请求成功", "data": response.text}
                    else:
                        result = {"code": "10", "message": "接口返回状态错误", "data": []}
                else:
                    result = {"code": "11", "message": "接口地址不能为空", "data": []}
            except Exception as e:
                result = {"code": "999", "message": "接口Get请求异常:" + e.message, "data": []}
    
            return result
    
        def __httpPost(self, api_url, header_data, api_param, env):
            try:
                if api_url != "":
                    response = requests.post(url=env + api_url, data=api_param, headers=header_data, timeout=10, verify=False)
                    if response.status_code == 200:
                        result = {"code": "00", "message": "接口Post请求成功", "data": response.text}
                    else:
                        result = {"code": "20", "message": "接口返回状态错误", "data": []}
                else:
                    result = {"code": "21", "message": "接口地址不能为空", "data": []}
            except Exception as e:
                result = {"code": "999", "message": "接口Post请求异常", "data": []}
    
            return result
    
        def httpRequest(self, req_type, api_url, header_data, api_param, env=readConfig.env):
            try:
                if req_type.lower() == "get":
                    result = self.__httpGet(api_url, header_data, api_param, env)
                elif req_type.lower() == "post":
                    result = self.__httpPost(api_url, header_data, api_param, env)
                else:
                    result = {"code": "30", "message": "请求类型错误", "data": []}
            except Exception as e:
                result = {"code": "999", "message": "系统异常", "data": []}
    
            log.info(result)
            return result
    
    
    if __name__ == "__main__":
        re = Request()
        op = operateDB.OperateDB()
        data = op.queryMany(
            "select * from case_interface where name_interface='getIpInfo' and  id=1")
    
        re.httpRequest(data["data"][0]["exe_mode"], data["data"][0]["url_interface"], eval(data["data"][0]["header_interface"]),
                       data["data"][0]["params_interface"])
    
    
    使用配置文件切换测试环境、预发环境、生产环境 config.ini
    [request_url]
    ;env_test = "test_url"
    ;env_public = "public_url"
    ;env_pre = "pre_url"
    env = ""
    
    配置文件读取 readConfig.py
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    __author__ = 'Jing'
    __mtime__ = 2017-11-12
    """
    
    import ConfigParser
    import os
    
    configPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.ini")
    conf = ConfigParser.ConfigParser()
    conf.read(configPath)
    
    env = conf.get("request_url", "env")
    
    host = conf.get("email", "host")
    user = conf.get("email", "user")
    pwd = conf.get("email", "pwd")
    
    if __name__ == "__main__":
        pass
    
    

    相关文章

      网友评论

          本文标题:基于Python的接口自动化测试(1)

          本文链接:https://www.haomeiwen.com/subject/plvzextx.html