common.py 7.09 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import configparser
import unittest
import time
from datetime import timedelta,date
from discover import DiscoveringTestLoader
from commons.Logging import log


def get_global_config(file,section, key):
    "object file is *src/config/global_data.conf"
    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):
    "object file is *src/config/api/*.conf"
    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 get_market_config(api,section,key):
    "object file is *src/config/marketConfig/*.conf"
    current_path=os.path.dirname(__file__)
    src_path=os.path.dirname(current_path)
    api_path=src_path+"/config/marketConfig/"
    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 run_one(name):
    test_suite = unittest.TestSuite()
    #创建测试套
    test_suite.addTest(name)
    #显示运行用例
    print("运行用例为{}".format(test_suite))
    runner = unittest.TextTestRunner()
    runner.run(test_suite)


def run_list(name):
    test_suite = unittest.TestSuite()
    test_suite.addTests(name)
    #显示运行用例
    print("运行用例为{}".format(test_suite))
    # with open(file="E:\\PycharmWorkspace\\jmsf-re-local\\report\\test.log", mode="a", encoding="utf-8") as file:
    #     runner = unittest.TextTestRunner(stream=file,verbosity=2)
    #     runner.run(test_suite)
    runner = unittest.TextTestRunner()
    runner.run(test_suite)

def run_class(name):
    test_cases = unittest.TestLoader().loadTestsFromTestCase(name)
    #显示运行用例
    print("运行用例为{}".format(test_cases))
    runner = unittest.TextTestRunner()
    runner.run(test_cases)
    
def run_Module(name):
    test_cases = unittest.TestLoader().loadTestsFromModule(name)
    #显示运行用例
    print("运行用例为{}".format(test_cases))
    runner = unittest.TextTestRunner()
    runner.run(test_cases)
    
def run_Name(name):
    test_cases = unittest.TestLoader().loadTestsFromName(name)
    #显示运行用例
    print("运行用例为{}".format(test_cases))
    runner = unittest.TextTestRunner()
    runner.run(test_cases)

def mylog(func):
    def RebackTest(self):
        log.info("{}".format(func.__name__))
        return func(self)
    return  RebackTest

def date_time_chinese():
    "returns the current time string,format for YYYY年mm月dd日HH时MM分SS秒"
    return time.strftime("%Y年%m月%d日 %H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())


def date_chinese():
    "returns the current time string, format for YYYY年mm月dd日"
    return time.strftime("%Y年%m月%d日".decode('utf-8').encode('gbk'),time.localtime())

def time_chinese():
    "returns the current time string,format for HH时 MM分 SS秒"
    return time.strftime("%H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())

def date_time():
    "returns the current time string, format for YYYY-mm-dd HH:MM:SS"
    return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

def date_time_slash():
    "returns the current time string,format for YYYY/mm/dd HH:MM:SS"
    return time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

def dates():
    "returns the current time string,format for YYYY-mm-dd"
    return time.strftime("%Y-%m-%d",time.localtime())

def date_slash():
    "returns the current time string,format for YYYY/mm/dd"
    return time.strftime("%Y/%m/%d",time.localtime())

def times():
    "returns the current time string, format for HH:MM:SS"
    return time.strftime("%H:%M:%S",time.localtime())

def year():
    "returns the current time string,format for year"
    return time.strftime("%Y",time.localtime())

def month():
    "returns the current time string,format for month"
    return time.strftime("%m",time.localtime())

def day():
    "returns the current time string,format for day"
    return time.strftime("%d",time.localtime())

def hour():
    "returns the current time string, format for Hour"
    return time.strftime("%H",time.localtime())

def minute():
    "returns the current time string,format for minute"
    return time.strftime("%M",time.localtime())

def seconds():
    "return the current time string,format for seconds"
    return time.strftime("%S",time.localtime())

def str_to_tuple(stime):
    "returns the string variable into time tuples"
    return time.strptime(stime,"%Y-%m-%d %H:%M:%S")

def add_date(day_num):
    today=date.today()
    print("returns the current date-%s and a time interval-%s" %(today,day_num))
    times=today+timedelta(days=day_num)
    return times

def sub_date(day_num):
    today=date.today()
    print("returns the current date-%s minus one time interval-%s" %(today,day_num))
    times=today-timedelta(days=day_num)
    return times

def time_stamp():
    #返回时间戳
    t=time.time()
    # print (t)                       #原始时间数据
    # print (int(t))                  #秒级时间戳
    # print (int(round(t * 1000)))    #毫秒级时间戳
    # print (int(round(t * 1000000))) #微秒级时间戳
    return int(round(t * 1000))