common.py 4.11 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import operator
import configparser
import requests
import json
import re
from commons.Logging import Logger
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
log=Logger()


def get_global_config(file,section, key):
    
    current_path=os.path.dirname(__file__)
    src_path=os.path.dirname(current_path)
#     global_setting_path=src_path+'/config/global_data.conf'
    global_setting_path=src_path+'/config/'+file+".conf"
    #验证文件是否存在
    file_path = os.path.exists(global_setting_path)
    if file_path:
    #获取文件的数据
        if isinstance(key,int):
                log.error("key of section cannot be int Type :<%r> "%str(key))
                raise Exception("key of section cannot be int Type :<%r> "%str(key))
        else:
            config = configparser.ConfigParser()
            config.read(global_setting_path,encoding="utf-8")       
            return config.get(section, key)
    else:
        log.error("File Not Exist :<%r> "%str(global_setting_path))
        raise Exception("File Not Exist :<%r> "%str(global_setting_path))


def get_api_config(api,section,key):
    current_path=os.path.dirname(__file__)
    src_path=os.path.dirname(current_path)
    api_path=src_path+"/config/api/"
    api_config_path=api_path+api+".conf"
    #验证文件是否存在
    file_path = os.path.exists(api_config_path)
    if file_path:
    #获取文件的数据
        if isinstance(key,int):
                log.error("key of section cannot be int Type :<%r> "%str(key))
                raise Exception("key of section cannot be int Type :<%r> "%str(key))
        else:
            config = configparser.ConfigParser()
            config.read(api_config_path,encoding="utf-8")       
            return config.get(section, key)
    else:
        log.error("File Not Exist :<%r> "%str(api_config_path))
        raise Exception("File Not Exist :<%r> "%str(api_config_path))


def set_priority(priority_list):
    #用户自定义优先级方法,设定有需要运行的用例优先级
    global _global_priority
    if priority_list:
        _global_priority=priority_list
        return _global_priority
    else:
        _global_priority=[1,2,3]
        return _global_priority

def set_mark(case_mark):
    #用户自定义优先级方法,设定有需要运行的用例优先级
    global _global_mark
    if case_mark:
        _global_mark=case_mark
        return _global_mark
    else:
        _global_mark=False
        return _global_mark

def read_priority_list():
    #需要重新设定一个方法来读取配置的全局优先级变量,且必须在全局变量初始化后使用,不然会报错
    priority_list=[1,2,3]
    try:
        return _global_priority
    except Exception as e:
        return priority_list

def read_case_mark():
    #需要重新设定一个方法来读取配置的全局优先级变量,且必须在全局变量初始化后使用,不然会报错
    casemark=False
    try:
        return _global_mark
    except Exception as e:
        return casemark

def mark(tag=None,p=None):
    #此处的try主要用于,当用户需要直接运行单个用例文件时,没提前_global_priority和_global_mark初始化
    try:
        #用户没有输入任何值
        if _global_priority == [1,2,3] and _global_mark == False :
            return True
        #只指定用例标签
        elif _global_priority == [1,2,3]  and _global_mark != False:
            if tag == _global_mark:
                return True
            else:
                return False
        #只指定用例优先级
        elif _global_priority != [1,2,3]  and _global_mark == False:
            if p in _global_priority:
                return True
            else:
                return False
        #同时指定优先级和标签
        elif _global_priority != [1,2,3]   and _global_mark != False:
            if p in _global_priority and tag == _global_mark:
                return True
            else:
                return False  
    except Exception as e:
        return True



# l=get_global_config("global_data", "email", "list")