Ejemplo n.º 1
0
def filled_other_field(excel_list):
    """
    14.响应信息:response_info
    15.实际的关键字段值:actual_core_field_value    < (Mongo)list -> (Excel)string >
    16.关键字段值比较结果:result_core_field_value
    17.响应字段列表比较结果:result_field_name_list
    18.测试结果:test_result
    19.创建时间:create_time   < (Mongo)ISODate -> (Excel)string >
    20.更新时间:update_time   < (Mongo)ISODate -> (Excel)string >

    【 填补其他的字段 】
    :param excel_list:
    :return:
    """
    current_iso_date = get_current_iso_date()
    for index, line_dict in enumerate(excel_list):
        line_dict["response_info"] = ""
        line_dict["actual_core_field_value"] = []
        line_dict["result_core_field_value"] = ""
        line_dict["result_field_name_list"] = ""
        line_dict["test_result"] = ""
        line_dict["create_time"] = current_iso_date
        line_dict["update_time"] = current_iso_date
    # show_excel_list(excel_list)
    return excel_list
Ejemplo n.º 2
0
def case_import_mongo(pro_name):
    """
    更新项目测试用例数据 同步入mongo库中,默认状态为'下线'
    :param pro_name:
    :return:
    【 备 注 】
    1.run_status :运行状态 ( pending 待运行、runninng 运行中、stopping 已停止)
    2.start_time :运行开始时间
    3.run_time :运行时间
    """
    test_class_list = get_test_class_list(pro_name)
    if test_class_list:
        insert_list = []
        test_loader = unittest.TestLoader()
        for test_class in test_class_list:
            test_methods_name = test_loader.getTestCaseNames(test_class)
            for test_method_name in test_methods_name:
                # 生成'测试方法'的实例对象,并反射获取'测试方法'
                test_instance = test_class(pro_name=pro_name,
                                           test_method=test_method_name)
                testMethod = getattr(test_instance, test_method_name)
                # 获取'测试方法'中的备注,作为'测试用例名称'
                test_case_name = testMethod.__doc__.split("\n")[0].strip()
                test_case_dict = {}
                test_case_dict["pro_name"] = pro_name
                test_case_dict["test_class_name"] = test_class.__name__
                test_case_dict["test_method_name"] = test_method_name
                test_case_dict["test_case_name"] = test_case_name
                test_case_dict["case_status"] = False
                test_case_dict["run_status"] = "stopping"
                test_case_dict["start_time"] = "----"
                test_case_dict["run_time"] = "----"
                test_case_dict["create_time"] = get_current_iso_date()
                insert_list.append(test_case_dict)
        # 将'测试用例'列表更新入对应项目的数据库中
        with MongodbUtils(ip=cfg.MONGODB_ADDR,
                          database=cfg.MONGODB_DATABASE,
                          collection=pro_name) as pro_db:
            try:
                pro_db.drop()
                pro_db.insert_many(insert_list)
            except Exception as e:
                mongo_exception_send_DD(e=e,
                                        msg="更新'" + pro_name + "'项目测试用例数据")
                return "mongo error"
        return insert_list
    else:
        return "no such pro"
Ejemplo n.º 3
0
def start_case_run_status(pro_name, test_method_name):
    """
    启动测试用例:设置用例的'运行状态=running'和'开始时间'
    :param pro_name:
    :param test_method_name:
    :return:
    """
    from Tools.mongodb import MongodbUtils
    with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db:
        try:
            query_dict = {"test_method_name": test_method_name}
            update_dict = {"$set": {"run_status": "running", "start_time": get_current_iso_date()}}
            pro_db.update(query_dict, update_dict, multi=True)
        except Exception as e:
            mongo_exception_send_DD(e=e, msg="启动'" + pro_name + "'项目中的测试用例")
            return "mongo error"
Ejemplo n.º 4
0
def import_mongodb(pro_name, excel_list, import_method):
    """
    将excel中的用例按照导入方式,导入mongo
    :param pro_name
    :param excel_list:
    :param import_method: 导入方式(batch_insert、all_replace、batch_insert_and_replace)
    :return:

    【 备 注 】
    all_replace:先清空数据库,然后全部插入
    batch_insert:先找出不在数据库中的用例列表,然后插入
    batch_insert_and_replace:先区分'不在数据库中的用例列表'和'需要更新的用例列表',分别执行插入和更新操作

    【 步 骤 】

    """
    with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db:
        try:

            if import_method == "all_replace":
                pro_db.drop()
                pro_db.insert_many(filled_other_field(excel_list))
            else:
                # 获取数据库中的'接口名称列表'
                all_case_in_db = pro_db.find()
                interface_name_list_in_db = [case.get("interface_name") for case in all_case_in_db]
                # 区分'不在数据库中的用例列表'和'需要更新的用例列表'
                update_list = []
                insert_list = []
                for index, line_dict in enumerate(excel_list):
                    if line_dict["interface_name"] in interface_name_list_in_db:
                        update_list.append(line_dict)
                    else:
                        insert_list.append(line_dict)
                print(insert_list)
                print(update_list)
                # 插入新增的数据
                if not is_null(insert_list):
                    pro_db.insert_many(filled_other_field(insert_list))
                # 更新数据
                if not is_null(update_list) and import_method == "batch_insert_and_replace":
                    for line_dict in update_list:
                        line_dict["update_time"] = get_current_iso_date()
                        query_dict = {"interface_name": line_dict["interface_name"]}
                        update_dict = {"$set": line_dict}
                        pro_db.update(query_dict, update_dict)