def get_test_case(pro_name): """ 根据项目获取测试用例列表(上线的排在前面) :param pro_name: :return: 返回值 """ test_case_list = [] on_line_list = [] off_line_list = [] with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: results = pro_db.find({}, {"_id": 0}) for res in results: test_case_dict = dict() test_case_dict["case_status"] = res.get("case_status") test_case_dict["test_case_name"] = res.get("test_case_name") test_case_dict["test_class_name"] = res.get("test_class_name") test_case_dict["test_method_name"] = res.get( "test_method_name") test_case_dict["run_status"] = res.get("run_status") test_case_dict["start_time"] = res.get("start_time") test_case_dict["run_time"] = res.get("run_time") test_case_dict["create_time"] = res.get("create_time") if res.get("case_status"): on_line_list.append(test_case_dict) else: off_line_list.append(test_case_dict) test_case_list = on_line_list + off_line_list except Exception as e: mongo_exception_send_DD(e=e, msg="获取'" + pro_name + "'项目测试用例列表") return "mongo error" finally: return test_case_list
def get_progress_info(pro_name): """ 获取项目用例执行进度信息:总执行数(上线数)、已执行数(上线数中'已停止'的数量)、进度百分比 :param pro_name: :return: """ with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: progress_info = dict() results = pro_db.find({"case_status": True}) on_line_run_status_list = [res.get("run_status") for res in results] run_num = len(on_line_run_status_list) if run_num == 0: progress_info["run_num"] = 0 progress_info["done_num"] = 0 progress_info["percent"] = 0 else: progress_info["run_num"] = run_num # 将 '上线的运行状态列表'中'run_status=stopping'的运行状态保存入列表 has_stop_run_status_list = [run_status for run_status in on_line_run_status_list if run_status == "stopping"] stop_num = len(has_stop_run_status_list) progress_info["done_num"] = stop_num # 计算百分比 percent = int(float(stop_num) / float(run_num) * 100) progress_info["percent"] = percent return progress_info except Exception as e: mongo_exception_send_DD(e=e, msg="获取'" + pro_name + "项目用例执行进度信息") return "mongo error"
def get_online_case_to_suite(pro_name, connected_android_device_list=[]): """ 将'测试类'列表中的'上线'的'测试方法'添加入 suite 实例对象中 :param pro_name: :param connected_android_device_list: 已连接设备信息列表 :return: 【 添 加 步 骤 】 1.从mongo中获取'上线'状态的'测试用例'列表 2.重置 上线用例的'运行状态:pending、开始时间:----、运行时间:----' 3.通过'项目名称'获取'测试类'列表 4.循环获取'测试类'列表中的所有'测试方法名称' 5.将这些'测试方法名称'与mongo中'上线'的'测试方法名称'作比较 6.匹配成功的,则实例化'测试类'时,并添加入'suite'实例对象中 【 备 注 】 实例化'测试类'时,必须带上该类中存在的以'test_'开头的方法名 """ with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: # 获取上线用例列表 query_dict = {"case_status": True} results = pro_db.find(query_dict, {"_id": 0}) on_line_test_method_name_list = [ result.get("test_method_name") for result in results ] # 重置 上线用例的'运行状态:pending、开始时间:----、运行时间:----' update_dict = { "$set": { "run_status": "pending", "start_time": "----", "run_time": "----" } } pro_db.update(query_dict, update_dict, multi=True) except Exception as e: on_line_test_method_name_list = [] mongo_exception_send_DD(e=e, msg="获取'" + pro_name + "'项目'上线'测试用例数据") return "mongo error", on_line_test_method_name_list from Config.pro_config import get_test_class_list test_class_list = get_test_class_list(pro_name) test_loader = unittest.TestLoader() suite = unittest.TestSuite() for test_class in test_class_list: test_methods_name = test_loader.getTestCaseNames(test_class) for test_method_name in test_methods_name: if test_method_name in on_line_test_method_name_list: # 匹配'测试方法'名称 test_instance = test_class(pro_name=pro_name, test_method=test_method_name, connected_android_device_list= connected_android_device_list) suite.addTest(test_instance) return suite, on_line_test_method_name_list
def download_file_by_name(self, file_name, out_name): """ 按文件名获取图片,保存到'out_name'中 :param file_name: :param out_name: :return: """ try: img_binary = self.fs.get_version(filename=file_name, version=1).read() with open(out_name, 'wb') as file_w: file_w.write(img_binary) except Exception as e: mongo_exception_send_DD(e=e, msg="获取图片")
def stop_case_run_status(pro_name): """ 强行修改项目用例运行状态 -> 停止 :param pro_name: :return: """ with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: update_dict = {"$set": {"run_status": "stopping"}} pro_db.update({}, update_dict, multi=True) return True except Exception as e: mongo_exception_send_DD(e=e, msg="强行修改'" + pro_name + "'项目用例运行状态") return "mongo error"
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"
def update_case_status_all(pro_name, case_status=False): """ 更新项目所有测试用例状态(上下线) :param pro_name: :param case_status: :return: 返回 test_method_name_list 列表 """ with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: update_dict = {"$set": {"case_status": case_status}} pro_db.update({}, update_dict, multi=True) results = pro_db.find({}, {"_id": 0}) return [res.get("test_method_name") for res in results] except Exception as e: mongo_exception_send_DD(e=e, msg="更新'" + pro_name + "'项目所有测试用例状态") return "mongo error"
def update_case_status(pro_name, test_method_name): """ 更新项目测试用例状态 :param pro_name: :param test_method_name: :return: """ 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} result = pro_db.find_one(query_dict, {"_id": 0}) old_case_status = result.get("case_status") new_case_status = bool(1 - old_case_status) # 布尔值取反 update_dict = {"$set": {"case_status": new_case_status}} pro_db.update_one(query_dict, update_dict) return new_case_status except Exception as e: mongo_exception_send_DD(e=e, msg="更新'" + pro_name + "'项目测试用例状态(单个)") return "mongo error"
def del_file_by_date(self, date_str): """ 删除指定日期之前的所有图片 :param date_str: 必须是'ISODate'类型的字符串 -> 2020-03-02T15:51:05 :return: {"uploadDate" : {"$lt": ISODate("2020-03-02T15:51:05")}} """ try: ISODate = parser.parse(date_str) query_dict = {"uploadDate": {"$lt": ISODate}} grid_outs = self.fs.find(query_dict) file_id_list = [] for grid_out in grid_outs: log.info(grid_out.__dict__) file_id_list.append(str(grid_out._file.get("_id"))) for file_id in file_id_list: self.fs.delete(file_id=ObjectId(file_id)) return len(file_id_list) except Exception as e: mongo_exception_send_DD(e=e, msg="删除图片") return None
def get_base64_by_id(self, file_id): """ 按文件'file_id'获取图片'base64码' :param file_id: :return: 1.获取成功 -> 返回 图片二进制文件 2.找不到该文件 -> 返回 no such file 3.mongo连接不上 -> 返回 None """ img_base64 = None try: gf = self.fs.get(file_id=ObjectId(file_id)) img_binary = gf.read() img_base64 = str(base64.b64encode(img_binary))[2:-1] except Exception as e: mongo_exception_send_DD(e=e, msg="获取二进制图片") if "Connection refused" not in str(e): img_base64 = "no such file" finally: # log.info("img_base64 : " + str(img_base64)) return img_base64
def get_case_run_status(pro_name): """ 获取项目用例的运行状态 :param pro_name: :return: """ case_run_status_list = [] with MongodbUtils(ip=cfg.MONGODB_ADDR, database=cfg.MONGODB_DATABASE, collection=pro_name) as pro_db: try: results = pro_db.find({"case_status": True}, {"_id": 0}) for res in results: test_case_dict = dict() test_case_dict["test_method_name"] = res.get("test_method_name") test_case_dict["run_status"] = res.get("run_status") test_case_dict["start_time"] = str(res.get("start_time")) test_case_dict["run_time"] = str(res.get("run_time")) case_run_status_list.append(test_case_dict) return case_run_status_list except Exception as e: mongo_exception_send_DD(e=e, msg="获取'" + pro_name + "'项目用例的运行状态") return "mongo error"
def upload_file(self, img_file_full): """ 上传图片 :param img_file_full: :return: 1.上传成功 -> 返回 图片id 2.mongo连接不上 -> 返回 None """ img_file = img_file_full.split("/")[-1] img_name = img_file.split(".")[0] img_tpye = img_file.split(".")[1] files_id = None try: with open(img_file_full, 'rb') as file_r: object_id = self.fs.put(data=file_r, content_type=img_tpye, filename=img_name) files_id = str(object_id) except Exception as e: mongo_exception_send_DD(e=e, msg="上传图片") finally: return files_id
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) except Exception as e: mongo_exception_send_DD(e=e, msg="导入'" + pro_name + "'项目excel测试用例数据") return "mongo error" 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: