common.py 3.1 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import configparser
import unittest
from discover import DiscoveringTestLoader
from commons.Logging import Logger
log=Logger()


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 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))
    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)