Exemple #1
0
class Runmain():
    def __init__(self):
        self.send_mail = SendEmail()

    def run_case(self):
        suite = unittest.TestSuite()

        # 2种用法:第一种suite.addTest()
        # suite.addTest(Case('test_case01'))
        # suite.addTest(Case('test_case02'))
        # suite.addTest(Test('test_01'))
        # suite.addTest(Test('test_02'))

        #2种用法:第二种suite.addTests()
        suite.addTests(map(Test, ["test_01", "test_02"]))
        suite.addTests(map(Case, ["test_case01", "test_case02"]))

        # # 输出结果:测试结果直接输出在控制台
        # unittest.TextTestRunner().run(suite)

        # 输出结果:将测试结果以report.html形式生成
        st = open('../report/report.html', 'wb')
        HTMLTestRunner.HTMLTestRunner(stream=st,
                                      title=u'接口自动化测试报告',
                                      description=u'测试者:shapeying').run(suite)

        #发送邮件带测试报告附件
        self.send_mail.send_main()
Exemple #2
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    #程序执行
    def go_on_run(self):
        res = None
        pass_count = []
        error_count = []
        rows_count = self.data.get_case_lines()
        #print rows_count
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            # print is_run
            if is_run:
                url = self.data.get_url(i)
                #print url
                method = self.data.get_request_method(i)
                #print method
                request_data = self.data.get_data_for_json(i)
                #print data
                expect = self.data.get_expect_data(i)
                print expect
                header = self.data.is_header(i)
                #print header
                depend_case = self.data.is_depend(i)
                #print depend_case_id
                res = self.run_method.run_main(method, url, request_data,
                                               header)
                #print res
                if depend_case != None:
                    self.depend_data = DependdentData(depend_case)
                    #获取依赖的响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    #获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    request_data[depend_key] = depend_response_data

                if self.com_util.is_contain(expect, res):
                    self.data.write_data(i, "PASS")
                    pass_count.append(i)
                    #print "PASS"
                else:
                    print self.data.write_data(i, res)
                    error_count.append(i)
        print len(pass_count)
        print len(error_count)
        self.send_mail.send_main(pass_count, error_count)
Exemple #3
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_email = SendEmail()

    def go_on_run(self):
        """程序执行"""
        pass_count = []
        fail_count = []
        no_run_count = []
        res = None
        # 获取用例数
        rows_count = self.data.get_case_lines()
        print(rows_count)
        # 第一行索引为0
        for i in range(1, rows_count):
            print("i:", i)
            is_run = self.data.get_is_run(i)
            print(is_run)
            if is_run:
                url = self.data.get_request_url(i)
                print("url:", url)
                method = self.data.get_request_method(i)
                print("method:", method)
                request_data = self.data.get_data_for_json(i)
                print("request_data:", request_data)
                print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", type(request_data))
                expect = self.data.get_expect_data(i)
                print("expect:", expect)
                header = self.data.is_header(i)
                print("header:", header)
                token = self.data.is_token(i)
                print("token:", token)
                depend_case = self.data.is_depend(i)
                print("depend_case:", depend_case)

                if depend_case != None:
                    self.depend_data = DependentData(depend_case)
                    # 获取依赖的响应数据
                    print("#### i", i)
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    print("##################")
                    print("1depend_response_data:", depend_response_data)
                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    print("depend_key:", depend_key)
                    # 更新请求字段
                    request_data.get(
                        'params')[depend_key] = depend_response_data
                    #request_data = json.dumps(request_data, ensure_ascii=False)
                    print("request_data:", request_data)
                    print(type(request_data))
                # 如果header字段值为write则将该接口的返回的token写入到token.json文件,如果为yes则读取token.json文件
                if header == "write_token":
                    print("********************")
                    res = self.run_method.run_main(method, url,
                                                   request_data).json()
                    print("res:", res)
                    print("res type", type(res))
                    #print("res.json():", res.json())
                    op_header = OperationHeader(res)
                    op_header.write_token()
                elif header == 'write_cookie' and token == 'yes':
                    print("case22222222222222")
                    op_json = OperationJson("../dataconfig/token.json")
                    token = op_json.get_data('data')
                    request_data = dict(request_data,
                                        **token)  # 把请求数据与登录token合并,并作为请求数据
                    print("login_data", request_data)
                    res = self.run_method.run_main(method, url, request_data)
                    print("res_login:"******"@@@@@@@@@@@@@@@@@@@@@@@@@@")
                    op_json1 = OperationJson('../dataconfig/cookie.json')
                    op_json2 = OperationJson('../dataconfig/token.json')
                    cookies = op_json1.get_data('cookie')
                    token = op_json2.get_data('data')
                    request_data = dict(request_data, **token)
                    print("post_data:", request_data)
                    print("cookie:", cookies)
                    new_value = json.dumps(request_data.get("params"))
                    request_data["params"] = new_value
                    res = self.run_method.run_main(method,
                                                   url,
                                                   request_data,
                                                   cookies=cookies)
                    print("res:", type(res))
                else:
                    res = self.run_method.run_main(method, url, request_data)

                if expect != None:
                    if self.com_util.is_contain(expect, res):
                        self.data.write_result(i, "Pass")
                        pass_count.append(i)
                        if type(res) is dict:
                            self.data.write_real_res(i, json.dumps(res))
                        else:
                            self.data.write_real_res(i, json.dumps(res.json()))
                    else:
                        self.data.write_result(i, "Fail")
                        fail_count.append(i)
                        if type(res) is dict:
                            self.data.write_real_res(i, json.dumps(res))
                        else:
                            self.data.write_real_res(i, json.dumps(res.json()))
                else:
                    print(f"用例ID:case-{i},预期结果不能为空")
            else:
                self.data.write_result(i, "Not run")
                no_run_count.append(i)

        # 发送邮件

        print(f"通过用例数:{len(pass_count)}")
        print(f"失败用例数:{len(fail_count)}")
        print(f"未执行败用例数:{len(no_run_count)}")

        self.send_email.send_main(pass_count, fail_count, no_run_count)
Exemple #4
0
class RunTest:

    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    # 程序执行
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_lines()

        # 每次执行用例之前将log日志文件清空数据
        log_file = '../log/log.txt'
        with open(log_file, 'w') as f:
            f.seek(0, 0)  # 加上f.seek(0),把文件定位到position 0;没有这句的话,文件是定位到数据最后,truncate也是从最后这里删除
            f.truncate()  # 清空数据

        # 循环执行每行用例
        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    # 获取请求参数
                    data = self.data.get_data_value(i)
                    # 获取excel文件中header关键字
                    header_key = self.data.get_request_header(i)
                    # 获取json文件中header_key对应的头文件数据
                    header = self.data.get_header_value(i)
                    expect = self.data.get_expect_data(i)
                    depend_case = self.data.is_depend(i)

                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        # 获取依赖字段的响应数据
                        depend_response_data = self.depend_data.get_value_for_key(i)
                        # 获取请求依赖的key
                        depend_key = self.data.get_depend_field(i)
                        # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数
                        data[depend_key] = depend_response_data

                    # cookie相关的没有跑通,代码逻辑是正常的,但是模拟登陆返回一直是非法请求
                    if header_key == 'write_Cookies':
                        res = self.run_method.run_main(method, url, data, header, params=data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()

                    elif header_key == 'get_Cookies':
                        op_json = OperationJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        res = self.run_method.run_main(method, url, data, header=cookies, params=data)

                    else:
                        res = self.run_method.run_main(method, url, data, header, params=data)

                    '''
                    get请求参数是params:request.get(url='',params={}),post请求数据是data:request.post(url='',data={})
                    excel文件中没有区分直接用请求数据表示,则data = self.data.get_data_value(i)拿到的数据,post请求就是data=data,get请就是params=data
                    '''

                    # excel中拿到的expect数据是str类型,但是返回的res是dict类型,两者数据比较必须都是字符类型
                    if self.com_util.is_contain(expect, json.dumps(res)):
                        self.data.write_result(i, 'pass')
                        pass_count.append(i)
                    else:
                        # 返回的res是dict类型,要将res数据写入excel中,需将dict类型转换成str类型
                        self.data.write_result(i, json.dumps(res))
                        with open(log_file, 'a', encoding='utf-8') as f:
                            f.write("\n第%s条用例实际结果与预期结果不一致:\n" % i)
                            f.write("Expected:%s\n  Actual:%s\n" % (expect, res))
                        fail_count.append(i)

                else:
                    no_run_count.append(i)

            except Exception as e:
                # 将异常写入excel的测试结果中
                self.data.write_result(i, str(e))
                # 将报错写入指定路径的日志文件里
                with open(log_file, 'a', encoding='utf-8') as f:
                    f.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)

        self.send_mail.send_main(pass_count, fail_count, no_run_count)