示例#1
0
def get_case(cli_commands):
    if cli_commands.suite:
        sui = unittest.TestSuite()
        for p in cli_commands.suite:
            p = os.path.join(ConfigManager.get_test_case_root(), p)
            if os.path.isdir(p):
                path_list = sorted([
                    os.path.join(root, name)
                    for root, dirs, files in os.walk(p) for name in files
                    if os.path.join(root, name).endswith('.py')
                    and not os.path.join(root, name).endswith('__init__.py')
                ])
                print(path_list)
                for path in path_list:
                    loader = unittest.TestLoader()
                    print(path)
                    path, file = os.path.split(os.path.abspath(path))
                    s = loader.discover(path, file)
                    sui.addTest(s)
            elif os.path.isfile(p):
                path, file = os.path.split(os.path.abspath(p))
                print(path, ":", file)
                s = unittest.defaultTestLoader.discover(path, file)
                sui.addTest(s)
    else:
        case_path = ConfigManager.get_test_case_root()
        sui = unittest.defaultTestLoader.discover(case_path, '*.py')
    return sui
示例#2
0
    def take_screen_shot():
        current_time = _time()
        (current_time_int, current_time_fraction) = divmod(current_time, 1)
        current_time_struct = _localtime(current_time_int)

        timestamp = _strftime("%Y-%m-%dT%H:%M:%S.", current_time_struct) + "%03d" % (
            int(current_time_fraction * 1000))
        method_name = getattr(TestLogger.current_test, '_testMethodName', '')
        exception_time = re.sub(r'[:.]', '-', timestamp)
        file_name = "%(method)s - %(time)s.png" % {'method': method_name, 'time': exception_time}
        from library.core.utils import ConfigManager
        path = os.path.join(ConfigManager.get_screen_shot_path(), file_name)
        if capture_screen_shot(path):
            print(timestamp + ' - INFO - ' + "截图路径:" + path)
            print('<img style="width:270px;height:480px;" src="' + path + '"  alt="用例执行失败时的截图" />')
示例#3
0
def run():
    os.environ.setdefault('AVAILABLE_DEVICES_SETTING', 'AVAILABLE_DEVICES')
    from library.core.utils import CommandLineTool
    cli_commands = CommandLineTool.parse_and_store_command_line_params()
    if cli_commands.deviceConfig:
        os.environ['AVAILABLE_DEVICES_SETTING'] = cli_commands.deviceConfig
    suite = get_case(cli_commands)
    from library.HTMLTestRunner import HTMLTestRunner
    report_path = ConfigManager.get_html_report_path()
    with common.open_or_create(report_path, 'wb') as output:
        runner = HTMLTestRunner(stream=output,
                                title='Test Report',
                                verbosity=2)
        result = runner.run(suite)

        # 成功、失败、错误、总计、通过率
        try:
            count = str(result.success_count + result.failure_count +
                        result.error_count)  # 总计
            pazz = str(result.success_count)  # 成功
            total_fail = str(result.failure_count + result.error_count)  # 失败
            if result.success_count + result.failure_count + result.error_count == 0:
                pazz_rate = '00.00%'
            else:
                rate = (result.success_count /
                        (result.success_count + result.failure_count +
                         result.error_count)) * 100
                pazz_rate = "%.2f%%" % rate
            from library.core.utils import send_report

            send_report.get_ui_automation_metric(count, pazz, total_fail,
                                                 pazz_rate)
            if cli_commands.sendTo:
                send_report.send_mail(*cli_commands.sendTo)
        except:
            msg = traceback.format_exc()
            print(msg)
            print("报告Email发送失败")
示例#4
0
from pip._internal import main as install_requirements

# 自动安装依赖
install_requirements(['install', '-r', 'requirements.txt'])

if __name__ == '__main__':
    os.environ.setdefault('AVAILABLE_DEVICES_SETTING', 'AVAILABLE_DEVICES')
    from library.core.utils import CommandLineTool

    cli_commands = CommandLineTool.parse_and_store_command_line_params()
    if cli_commands.deviceConfig:
        os.environ['AVAILABLE_DEVICES_SETTING'] = cli_commands.deviceConfig

    from library.core.utils import ConfigManager, common

    report_path = ConfigManager.get_html_report_path()
    s = cli_commands.suite
    if cli_commands.suite:
        suite = None
        for p in cli_commands.suite:
            if os.path.isdir(p):
                s = unittest.defaultTestLoader.discover(
                    os.path.abspath(p), '*.py')
            elif os.path.isfile(p):
                path, file = os.path.split(os.path.abspath(p))
                s = unittest.defaultTestLoader.discover(path, file)
            else:
                raise ValueError(
                    'Path "{}" is not an valid file path!'.format(p))
            if suite is None:
                suite = s
示例#5
0
import os
import traceback
import unittest
from library.core.utils import ConfigManager, common

module_list = os.popen('pip freeze').readlines()
require_file = os.path.join(ConfigManager.get_project_path(),
                            'requirements.txt')
with open(require_file, 'r') as f:
    require_list = f.readlines()
    for require in require_list:
        if require not in module_list:
            os.system('pip install -r requirements.txt')
            break


def get_case(cli_commands):
    if cli_commands.suite:
        sui = unittest.TestSuite()
        for p in cli_commands.suite:
            p = os.path.join(ConfigManager.get_test_case_root(), p)
            if os.path.isdir(p):
                path_list = sorted([
                    os.path.join(root, name)
                    for root, dirs, files in os.walk(p) for name in files
                    if os.path.join(root, name).endswith('.py')
                    and not os.path.join(root, name).endswith('__init__.py')
                ])
                print(path_list)
                for path in path_list:
                    loader = unittest.TestLoader()