Beispiel #1
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.is_contain = CommonUtil()
        self.log = UserLog()
        self.logger = self.log.get_log()

    # 执行程序
    def go_no_run(self):
        rows_conut = self.data.get_case_lines()  # 获取行数

        for i in range(1, rows_conut):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_url(i)
                case_name = self.data.get_case_name(i)
                method = self.data.get_request_way(i)  # 获取请求的方法
                request_data = self.data.get_json_data(i)  # 获取请求数据
                data_type = self.data.get_data_type(i)
                cookie = self.data.get_is_cookies(i)  # 获取cookies
                expect = self.data.get_expect(i)  # 获取预期结果
                depend_case = self.data.get_depend_case_id(i)  # 获取依赖的caseid
                if depend_case is not None:
                    # 获取替换请求字段
                    depend_key = self.data.get_depned_key(i)
                    # 获取依赖的响应数据
                    self.depend_data = DependData(depend_case)
                    depend_response_data = self.depend_data.get_depned_value(i)
                    split = len(depend_key.split(">"))  # 切割替换的字段
                    # 根据请求字段的个数替换
                    for y in range(0, split):
                        re = depend_key.split(">")
                        request_data[re[y]] = depend_response_data[y]
                if cookie == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_cookie = WriteCookies(res)  # 获取cookies
                    op_cookie.write_cookie()  # 写入cook ies
                elif cookie == 'yes':
                    get_cookies = OperationJson('../data/scm-cookies.json')
                    coo = get_cookies.get_data('SESSION')

                    cooki = {"SESSION": coo}
                    res = self.run_method.run_main(method, url, request_data,
                                                   cooki, data_type)

                else:
                    # get_cookies = OperationJson('../data/scm-cookies.json')
                    # cooki = get_cookies.get_data('accessToken')
                    # request_data['accessToken'] = cooki
                    res = self.run_method.run_main(method, url, request_data)

                if self.is_contain.is_contain(expect, res):
                    self.data.write_data(i, 'pass')
                    self.logger.info("第" + str(i) + "个case-->" + case_name +
                                     ": 测试通过")
                    self.logger.info("url--> " + url)
                    self.logger.info("request_data-->" + str(request_data))
                    self.logger.info("response_data-->" + res)
                    print("第" + str(i) + "个case-->" + case_name + ": 测试通过")
                    print(res)
                else:
                    self.data.write_data(i, res)
                    self.logger.info("第" + str(i) + "个case-->" + case_name +
                                     ": 测试失败")
                    self.logger.info("url--> " + url)
                    self.logger.info("request_data-->" + str(request_data))
                    self.logger.info("response_data-->" + res)
                    print("第" + str(i) + "个case-->" + "测试失败")
                    print(res)
Beispiel #2
0
 def __init__(self):
     self.operation = OperationExcel()
     log = UserLog()
     self.logger = log.get_log()
Beispiel #3
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.is_contain = CommonUtil()
        self.log = UserLog()
        self.logger = self.log.get_log()

    # 执行程序
    def go_no_run(self):
        rows_conut = self.data.get_case_lines()  # 获取行数

        for i in range(1, rows_conut):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_url(i)
                case_name = self.data.get_case_name(i)
                method = self.data.get_request_way(i)  # 获取请求的方法
                request_data = self.data.get_json_data(i)  # 获取请求数据
                data_type = self.data.get_data_type(i)
                cookie = self.data.get_is_cookies(i)  # 获取cookies
                expect = self.data.get_expect(i)  # 获取预期结果
                depend_case = self.data.get_depend_case_id(i)  # 获取依赖的caseid
                if depend_case is not None:
                    # 获取替换请求字段
                    depend_key = self.data.get_depned_key(i)
                    # 获取依赖的响应数据
                    self.depend_data = DependData(depend_case)
                    depend_response_data = self.depend_data.get_depned_value(i)
                    split = len(depend_key.split(">"))  # 切割替换的字段
                    # 根据请求字段的个数替换
                    for y in range(0, split):
                        re = depend_key.split(">")
                        request_data[re[y]] = depend_response_data[y]
                if cookie == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_cookie = WriteCookies(res)  # 获取cookies
                    op_cookie.write_cookie()  # 写入cook ies
                elif cookie == 'yes':
                    father_path = os.path.join(
                        os.path.dirname(
                            os.path.dirname(os.path.abspath(__file__))),
                        'data')
                    file_path = os.path.join(father_path, '金城-cookies.json')
                    get_cookies = OperationJson(file_path)

                    cooki = get_cookies.get_data('accessToken')
                    request_data['accessToken'] = cooki
                    res = self.run_method.run_main(method, url, request_data,
                                                   data_type)

                else:
                    father_path = os.path.join(
                        os.path.dirname(
                            os.path.dirname(os.path.abspath(__file__))),
                        'data')
                    file_path = os.path.join(father_path, '金城-cookies.json')
                    get_cookies = OperationJson(file_path)
                    cooki = get_cookies.get_data('accessToken')
                    request_data['accessToken'] = cooki
                    res = self.run_method.run_main(method, url, request_data)

                if self.is_contain.is_contain(expect, res):
                    self.data.write_data(i, 'pass')
                    # self.logger.info("第" + str(i) + "个case-->" + case_name + ": \033[31m测试通过\033[0m")
                    self.logger.info("第%s 个case-->%s: 测试通过" %
                                     (str(i), case_name))
                    self.logger.info("url--> " + url)
                    self.logger.info("request_data-->" + str(request_data))
                    self.logger.info("response_data-->" + res)
                    print("第" + str(i) + "个case-->" + case_name + ": 测试通过")
                    # print("第" + str(i) + "个case-->" + case_name + ": \033[32m测试通过\033[0m")

                else:
                    self.data.write_data(i, res)
                    self.logger.info("第" + str(i) + "个case-->" + case_name +
                                     ": 测试失败")
                    self.logger.info("url--> " + url)
                    self.logger.info("request_data-->" + str(request_data))
                    self.logger.info("response_data-->" + res)
                    # print("第" + str(i) + "个case-->" + "\033[35m测试失败\033[0m")
                    print("第%s 个case-->%s: 测试失败" % (str(i), case_name))
Beispiel #4
0
class KeywordCase(object):
    def run_main(self):
        self.action_metnod = ActionMethod()
        handle_excel = ExcelUtil(
            r"C:\Users\Administrator\PycharmProjects\AuToTest4\moco\case\key_word.xls"
        )
        # 拿到行数
        case_lines = handle_excel.get_lines()
        self.log = UserLog()
        self.logger = self.log.get_log()
        # 循环行数,执行每一行case
        if case_lines:
            for i in range(1, case_lines):
                is_run = handle_excel.get_col_value(i, 3)
                # print(is_run)
                # 是否执行
                if is_run == 'yes':
                    # 拿到执行方法
                    method = handle_excel.get_col_value(i, 4)
                    # 拿到输入数据
                    send_value = handle_excel.get_col_value(i, 5)
                    # 拿到操作元素
                    handle_value = handle_excel.get_col_value(i, 6)
                    # 获取预期结果的方法
                    except_result_method = handle_excel.get_col_value(i, 7)
                    # 获取预期结果的值
                    except_result = handle_excel.get_col_value(i, 8)
                    self.run_method(method, send_value, handle_value)
                    # 判断预期结果是否为空
                    if except_result != '':
                        except_value = self.get_except_result_value(
                            except_result)
                        if except_value[0] == 'text':

                            result = self.run_method(except_result_method)
                            if except_value[1] in result:
                                self.logger.info("测试通过")
                                handle_excel.write_value(i, 'pass')
                            else:
                                handle_excel.write_value(i, 'fail')
                        elif except_value[0] == 'element':
                            result = self.run_method(except_result_method,
                                                     except_value[1])
                            if result:
                                self.logger.info("测试通过")
                                handle_excel.write_value(i, 'pass')
                            else:
                                handle_excel.write_value(i, 'fail')
                        else:
                            return None

    # 获取预期结果
    def get_except_result_value(self, data):
        return data.split("=")

    # 运行的方法、输入数据和操作元素连在一起
    def run_method(self, method, send_value='', handle_value=''):
        method_value = getattr(self.action_metnod, method)
        # 是否有输入数据
        if send_value == '' and handle_value != '':
            # 执行方法(输入数据,操作元素)
            result = method_value(handle_value)
        # 如果没有输入数据
        elif send_value != '' and handle_value == '':
            # 执行方法(操作元素)
            result = method_value(send_value)
        elif send_value != '' and handle_value != '':
            result = method_value(send_value, handle_value)
        else:
            result = method_value()
        return result
Beispiel #5
0
class RunTest:
    def __init__(self):

        self.run_method = RunMethod()
        self.data = GetData()
        self.containt = CommonUtile()
        self.email = Mailer()
        self.log = UserLog()
        self.logger = self.log.get_log()

    # 执行的程序
    def go_on_run(self):
        res = None
        pass_conut = []
        fail_conut = []
        rows_conut = self.data.get_case_lines()
        for i in range(1, rows_conut):
            is_run = self.data.get_is_run(i)
            if is_run:
                self.request_type = self.data.get_request_type(i)

                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                request_data = self.data.get_json_data(i)
                case_name = self.data.get_case_name(i)
                cookie = self.data.get_cookies_value(i)
                expect = self.data.get_expect_data(i)
                depent_case = self.data.case_id(i)  # 获取依赖字段的key
                if depent_case is not None:
                    # 获取依赖的key
                    depent_key = self.data.get_depnet_file(i)
                    self.depend_data = DepnedentData(depent_case)
                    # 获取依赖的响应数据
                    depnet_response_data = self.depend_data.get_data_for_key(i)
                    le = len(depent_key.split(">"))  # 切分字符串取大小
                    for y in range(0, le):
                        re = depent_key.split(">")
                        request_data[re[y]] = depnet_response_data[y]  # 循环替换
                if cookie == 'write':
                    # 发送请求
                    res = self.run_method.run_main(method, url, request_data)
                    op_cookies = OperationHeader(res)
                    op_cookies.write_cookie()  # 写入cookies到cookie.json文件中
                elif cookie == 'yes':
                    get_cookie = OperationJson('../dataconfig/cookie.json')
                    cooki = get_cookie.get_data('SESSION')
                    cookies = {
                        "SESSION": cooki
                    }
                    res = self.run_method.run_main(method, url, request_data, cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                if self.containt.is_contain(expect, res):
                    self.data.write_result(i, 'pass')
                    pass_conut.append(i)
                    self.logger.info("测试通过")
                    self.logger.info("通过响应数据" + res)
                    print("测试通过")
                else:
                    self.logger.info("测试失败")
                    self.logger.info("失败case名称:" + case_name)
                    self.logger.info("失败的行数:" + str(i))
                    self.data.write_result(i, res)
                    fail_conut.append(i)
                    print("测试失败")
                    self.logger.info("url:" + url)
                    self.logger.info("失败请求数据:" + str(request_data))
                    self.logger.info("失败响应数据:" + res)

        # 发送测试报告邮件
        self.email.send_main(pass_conut, fail_conut)
Beispiel #6
0
class Mailer(object):
    def __init__(self):
        self.log = UserLog()
        self.logger = self.log.get_log()

        self.email_host = "smtp.163.com"
        self.send_user = "******"
        self.password = "******"

    def sendMail(self, mail_list, mail_title, mail_content):
        me = "安晓东" + "<" + self.send_user + ">"  # 发送者
        # me = self.mail_user + "<" + self.mail_user + "@" + self.mail_postfix + ">"
        msg = MIMEMultipart()
        msg['Subject'] = mail_title
        msg['From'] = me
        msg['To'] = ",".join(mail_list)

        # puretext = MIMEText('<h1>你好,<br/>'+self.mail_content+'</h1>', 'html', 'utf-8')
        puretext = MIMEText('运行结果:' + mail_content)
        msg.attach(puretext)

        # jpg类型的附件
        # jpgpart = MIMEApplication(open('/home/mypan/1949777163775279642.jpg', 'rb').read())
        # jpgpart.add_header('Content-Disposition', 'attachment', filename='beauty.jpg')
        # msg.attach(jpgpart)

        # 首先是xlsx类型的附件
        # te1 = os.path.join(os.path.dirname(os.getcwd()))  获取当前路径的上一层路径
        # te = os.path.join(os.getcwd())  获取当前路径
        # print(te1)
        # print(te)
        xlsxpart = MIMEApplication(
            open(
                r"C:\Users\Administrator\PycharmProjects\Interface\dataconfig\interface.xlsx",
                'rb').read())
        xlsxpart.add_header('Content-Disposition',
                            'attachment',
                            filename='interface.xlsx')
        msg.attach(xlsxpart)

        # mp3类型的附件
        # mp3part = MIMEApplication(open('kenny.mp3', 'rb').read())
        # mp3part.add_header('Content-Disposition', 'attachment', filename='benny.mp3')
        # msg.attach(mp3part)

        # pdf类型附件
        # part = MIMEApplication(open('foo.pdf', 'rb').read())
        # part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
        # msg.attach(part)

        try:
            s = smtplib.SMTP()  # 创建邮件服务器对象
            s.connect(self.email_host)  # 连接到指定的smtp服务器。参数分别表示smpt主机和端口
            s.login(self.send_user, self.password)  # 登录到你邮箱
            s.sendmail(me, mail_list, msg.as_string())  # 发送内容
            s.close()
            self.logger.info("邮件发送成功")
            return True
        except Exception as e:
            print(str(e))
            self.logger.info("邮件发送失败")
            return False

    def send_main(self, pass_list, fail_list):
        pass_num = float(len(pass_list))
        fail_num = float(len(fail_list))
        conut_num = pass_num + fail_num
        pass_num1 = len(pass_list)
        fail_num1 = len(fail_list)
        pass_result = "%.2f%%" % (pass_num / conut_num * 100)
        fail_result = "%.2f%%" % (fail_num / conut_num * 100)
        mail_list = [
            '*****@*****.**', '*****@*****.**', '*****@*****.**'
        ]
        mail_title = "接口自动化测试报告"
        mail_content = "此次运行接口用例为%d个,通过%d个,失败%d个,通过率%s,失败率%s" % (
            int(conut_num), int(pass_num1), int(fail_num1), pass_result,
            fail_result)
        self.logger.info("运行结果:" + mail_content)
        self.sendMail(mail_list, mail_title, mail_content)