def local_action(): # 若临时目录不存在则创建 mkdir(tmp_path) # 清空临时目录中的内容, 将项目代码拷贝入临时文件夹 with lcd(tmp_path): local("rm -rf " + pro_name) local("rm -rf " + deploy_file) local("cp -r " + pro_path + " " + tmp_path) # 删除临时文件夹中不需要的文件目录 with lcd(pro_tmp_path): local("rm -rf .DS_Store") local("rm -rf .git") local("rm -rf .gitignore") local("rm -rf .idea") local("rm -rf Logs") local("rm -rf Reports") local("rm -rf Screenshots") local("rm -rf vassals_local") local("rm -rf venv") local("rm -rf gulpfile.js") local("rm -rf package.json") local("rm -rf package-lock.json") local("rm -rf node_modules") local("rm -rf nohup.out") local("rm -rf tmp_uwsgi_pid.txt") local("ls") # 归档压缩 临时文件夹中的项目( 可以不进入目录,直接执行 ) with lcd(tmp_path): local("tar -czvf " + pro_name_tar + " " + pro_name) # 将部署文件上传服务器 with settings(host_string="%s@%s:%s" % (user, host, port), password=passwd): put(remote_path=remote_tmp_path, local_path=deploy_file)
def generate_report(pro_name, suite, title, description, tester, verbosity=1): """ 生 成 测 试 报 告 备注:每次生成的报告都存放在../logs/history/中,同时替换../logs/下的report.html :param pro_name: :param suite: suite 实例对象(包含了所有的测试用例实例,即继承了'unittest.TestCase'的子类的实例对象 test_instance ) :param title: :param description: :param tester: :param verbosity: 结果显示的详细程度 :return: """ print("实例对象suite是否存在'thread_num'属性:" + str(hasattr(suite, "thread_num"))) print("实例对象suite是否存在'screen_shot_id_dict'属性:" + str(hasattr(suite, "screen_shot_id_dict"))) print("实例对象suite是否存在'run_test_custom'方法:" + str(hasattr(suite, "run_test_custom"))) print("实例对象suite是否存在'show_result_custom'方法:" + str(hasattr(suite, "show_result_custom"))) print("实例对象suite是否存在'run'方法:" + str(hasattr(suite, "run"))) print(suite) print(suite.__class__) print(suite.__class__.__base__) print("+++++++++++++++++++++++++++++++++++") now = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime(time.time())) current_report_name = "[Android_report]" + pro_name + "[" + now + "].html" pro_report_path = cfg.REPORTS_DIR + pro_name + "/" history_report_path = pro_report_path + "history/" mkdir(history_report_path) current_report_file = history_report_path + current_report_name with open(current_report_file, 'wb') as fp: runner = HTMLTestRunner(stream=fp, title=title, description=description, tester=tester, verbosity=verbosity) test_result = runner.run(suite) # 将最新报告替换../Reports/{{pro_name}}/下的[Android_report]{{pro_name}}.html res = os.system("cp " + current_report_file + " " + pro_report_path + " && " "mv " + pro_report_path + current_report_name + " " + pro_report_path + "[Android_report]" + pro_name + ".html") if res != 0: log.error("测试报告替换操作有误!") # log.info(test_result) # log.info("执行总数:" + str(test_result.error_count + test_result.success_count + test_result.failure_count)) # log.info("执行的用例列表:" + str(test_result.result)) # log.info("错误数:" + str(test_result.error_count)) # log.info("错误的用例列表:" + str(test_result.errors)) # log.info("失败数:" + str(test_result.failure_count)) # log.info("失败的用例列表:" + str(test_result.failures)) # log.info("成功数:" + str(test_result.success_count)) # log.info("成功的用例列表:" + str([success[1] for success in test_result.result if success[0] == 0])) # log.info(suite.screen_shot_id_dict) return test_result, current_report_file
def screenshot(self, image_name, case_instance): """ 截 图、保 存 mongo、记录图片ID :param image_name: 图片名称 :param case_instance: 测试类实例对象 :return: """ current_test_path = cfg.SCREENSHOTS_DIR + case_instance.pro_name + "/" + case_instance.class_method_path # ../类名/方法名/ mkdir(current_test_path) self.driver.save_screenshot(current_test_path + image_name) mgf = MongoGridFS() files_id = mgf.upload_file(img_file_full=current_test_path + image_name) case_instance.screen_shot_id_list.append(files_id)
def screenshot(self, image_name): """ 截 图、保 存 mongo、记录图片ID :param image_name: 图片名称 【 使 用 case_instance 逻 辑 】 1.若'Base类的子类实例对象'调用该方法(在 object_page 中使用):则使用该实例对象本身的 self.case_instance 属性(测试用例实例对象) 2.若'Base类'调用该方法(在 test_case 中使用):则使用该 self 测试用例实例对象本身 3.由于'Base'类和'测试用例类'都含有'driver'属性,所以不影响self.driver的使用 :return: """ # 判断当前的'实例对象'是否是'Base'类型(考虑子类的继承关系) case_instance = isinstance(self, Base) and self.case_instance or self # 获取当前测试用例的路径 -> ../类名/方法名/ current_test_path = cfg.SCREENSHOTS_DIR + case_instance.pro_name + "/" + case_instance.class_method_path mkdir(current_test_path) self.driver.get_screenshot_as_file(current_test_path + image_name) mgf = MongoGridFS() files_id = mgf.upload_file(img_file_full=current_test_path + image_name) case_instance.screen_shot_id_list.append(files_id)