Beispiel #1
0
 def __init__(self):
     self.curDateTime = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) #当前日期时间
     self.config = scripts.get_yaml_field(os.path.join(gl.configPath,'config.yaml')) #配置文件路径
     self.sender = self.config['EMAIL']['Smtp_Sender'] # 从配置文件获取,发件人
     self.receivers = self.config['EMAIL']['Receivers']  # 从配置文件获取,接收人
     self.msg_title = self.config['EMAIL']['Msg_Title'] #从配置文件获取,邮件标题
     self.sender_server = self.config['EMAIL']['Smtp_Server'] #从配置文件获取,发送服务器
     self.From = self.config['EMAIL']['From']
     self.To = self.config['EMAIL']['To']
Beispiel #2
0
    def invoke():
        """
        Start executing tests generate test reports.
        :return: There is no.
        """
        # #########################Read configuration information###############
        config = get_yaml_field(gl.configFile)
        dd_enable = config['ENABLE_DDING']
        dd_token = config['DD_TOKEN']
        dd_url = config['DING_URL']
        email_enable = config['EMAIL_ENABLE']
        ########################################################################

        # Test report file name.
        time_str = time.strftime('%Y%m%d_%H%M%S', time.localtime())
        filePath = Run_Test_Case.create_report_file()

        # Start test the send pin message.
        if dd_enable:
            scripts.send_msg_dding('{}:★开始API接口自动化测试★'.format(time_str),
                                   dd_token, dd_url)

        # Execute the test and send the test report.
        Run_Test_Case.run(filePath)

        print(filePath)
        # Copy the folder under the report directory under  /templates/report/
        # low_path = Run_Test_Case.copy_report(filePath, Run_Test_Case.file_name)

        if dd_enable:
            # Template message.
            dir_list = filePath.split('\\')
            low_path = dir_list[len(dir_list) - 2]
            msg = Run_Test_Case.tmpl_msg(low_path, Run_Test_Case.file_name)
            print(msg)
            scripts.send_msg_dding(msg, dd_token, dd_url)

        if email_enable:
            # Send test report to EMAIL.
            email = EmailClass()
            email.send(filePath)
Beispiel #3
0
    def tmpl_msg(low_path, file_name):
        # 发送钉钉模版测试结果
        config = get_yaml_field(gl.configFile)
        # report外网发布地址ip+port
        report_url = config['REPORT_URL']
        # 钉钉标题
        content = config['DING_TITLE']
        # 从报告中取得测试结果数据 e.g. 3 tests; 2.23 seconds; 3 passed; 0 failed; 0 errors
        file_result = os.path.join(gl.loadcasePath, 'result.txt')
        #
        result_content = read_file(file_result, 'r')
        # Remove file
        remove_file(file_result)

        res_list = result_content.split(";")

        # 发送钉钉消息
        msg = """{}执行【已完成】:\n共{}个用例, 执行耗时{}秒, 通过{}, 失败{}, 错误{}, 通过率{}\n测试报告: {}/{}"""
        msg = msg.format(content, res_list[0], res_list[1],
                         res_list[2], res_list[3], res_list[4],
                         res_list[5], report_url, low_path)
        return msg
Beispiel #4
0
def _parse_config(config):
    """
    Parse config parameters.
    """
    if config:
        # Setting global var.
        if config[0] == 'set' and config.__len__() == 1:
            try:
                os.system(gl.configFile)
            except (KeyboardInterrupt, SystemExit):
                print("已终止执行.")
        elif config[0] == 'set' and config.__len__() == 2 and '=' in config[1]:
            conf = config[1].split("=")
            update_yam_content(gl.configFile, conf[0], parse_string_value(conf[1]))

        elif config[0] == 'get' and config.__len__() == 2 and '=' not in config[1]:
            content = get_yaml_field(gl.configFile)
            try:
                print(content[config[1]])
            except KeyError as ex:
                print('[KeyError]: {}'.format(ex))
        else:
            print('Unknown command: {}'.format(config[0]))
Beispiel #5
0
    def tmpl_msg(low_path, file_name):
        # 发送钉钉模版测试结果
        result_str = """共{}个用例, 通过{}, 失败{}, 错误{}, 通过率{}""".format(
            gl.get_value('sum'), gl.get_value('passed'),
            gl.get_value('failed'), gl.get_value('error'),
            gl.get_value('passrate'))

        # 测试结论
        if '100' in str(gl.get_value('passrate')):
            msg_1 = '本次测试★通过★'
        else:
            msg_1 = '本次测试★不通过★'

        config = get_yaml_field(gl.configFile)
        # report外网发布地址ip+port
        report_url = config['REPORT_URL']
        content = config['DING_TITLE']
        # 发送钉钉消息
        msg = """{}已完成:{},{}\n测试报告地址:{}/{}/{}"""
        msg = msg.format(content, result_str, msg_1, report_url, low_path,
                         file_name)

        return msg
Beispiel #6
0
    def run(filePath):
        """
        Execute the test and generate the test report file.
        Args:
            fileath: Report file absolute path.
        Return:
            There is no return.
        """
        config = get_yaml_field(gl.configFile)
        exe_con = config['ENABLE_EXECUTION']
        exe_num = config['EXECUTION_NUM']
        rerun = config['ENABLE_RERUN']
        renum = config['RERUN_NUM']  
        repeat = config['ENABLE_REPEAT']
        repeat_num = config['REPEAT_NUM']
        exec_mode = config['ENABLE_EXEC_MODE']
        debug_mode = config['ENABLE_DEBUG_MODE']

        # custom function
        RunTestCase.copy_custom_function()

        # Enable repeat case.
        peatargs = ''
        if repeat:
            peatargs = ' --count={} '.format(repeat_num)

        # Enable CPU concurrency
        pyargs = ''
        if exe_con:
            pyargs = ' -n {} '.format(exe_num)

        # Enable failed retry
        reargs = ''
        if rerun:
            reargs = ' --reruns {} '.format(renum)

        # debug mode print debug info.
        if not debug_mode:
            debug = '--tb=no'
        else:
            debug = ''

        """
        Load the pytest framework,
        which must be written here or DDT will be loaded first.
        from httptesting.case import test_load_case
        """
        case_path = gl.loadcasePath
        # Output mode console or report.
        if exec_mode:
            cmd = 'cd {} && py.test -q -s {} {} {} {}'.format(
                case_path, reargs, 'test_load_case.py',
                peatargs, debug
            ) 
        else:
            cmd = 'cd {} && py.test {} {} {} {} --html={} {} --self-contained-html'.format(
                case_path, pyargs, reargs, 'test_load_case.py', 
                peatargs, filePath, debug
            )
        try:
            os.system(cmd)
        except (KeyboardInterrupt, SystemExit):
            print('已终止执行.')
Beispiel #7
0
 def __init__(self):
     self.config = get_yaml_field(gl.configFile)
     self.base_url = self.config['BASE_URL']
     self.OUT_TMPL = """{0} {1}请求:{2}\n请求:\n{3}\n响应:"""