def trigger(self, data): """ # 这里创建一个空report,然后使用celery异步运行任务, # 当celery执行完毕后使用空report的id更新报告。 :param data: :return: """ # 通过接口传递过来的suite id来查询需要运行的接口。 id = data.get('id') interface = InterfaceModel.query.get(id) # 如果不存在接口则直接返回 if not interface: return # 创建空的report并提交 if 'team' not in data or not data['team']: data['team'] = interface.team if 'project' not in data or not data['project']: data['project'] = interface.team report_service = ReportService() report = report_service.empty_report(data) report = report.to_dict() cases = [interface.to_dict()] # 使用celery异步运行的接口任务。 interface_task.apply_async(args=(cases, data, report)) return report.get('id')
def interface_task(cases, data, report): from clover import app with app.test_request_context(): executor = Executor(log=report['id']) executor.execute(cases, data) try: if 'report' in data and data['report']: #正常传了report,平台触发的 name = data['report'] elif data['report'] == '' and data[ 'name'] != '': #没写report名,但是有套件名,平台触发的 name = data['name'] except Exception as error: name = 'ci-AutoTest' #name = data['report'] if 'report' in data and data['report'] else data['name'] update_report = { 'id': report['id'], 'team': data['team'], 'project': data['project'], 'name': name, 'type': 'interface', 'interface': executor.interface, 'start': executor.start, 'end': executor.end, 'duration': (executor.end - executor.start).total_seconds(), 'platform': get_system_info(), 'detail': executor.result, } report_service = ReportService() report_service.update(update_report) events = 'success' if executor.interface[ 'passed'] == executor.interface['total'] else 'failed' notify(update_report, events) return report
class ReportView(CloverView): def __init__(self): super(ReportView, self).__init__() self.service = ReportService() @catch_exception def create(self): """ :return: """ pass @catch_exception def delete(self): """ :return: """ if request.method == 'GET': data = request.values.to_dict() else: data = request.get_json() if 'id' not in data or not data['id']: return jsonify({ 'status': 400, 'message': '删除测试报告时缺少必须的测试报告ID参数', 'data': data, }) try: self.service.delete(data) return jsonify({ 'status': 0, 'message': '成功测试报告{0}'.format(data['id']), 'data': {'id': data['id']}, 'total': 1 }) except Exception as error: return jsonify({ 'status': 500, 'message': '服务异常,请联系管理员!', 'data': str(error) }) @catch_exception def update(self): pass @catch_exception def search(self): """ :return: """ if request.method == 'GET': data = request.values.to_dict() else: data = request.get_json() try: count, results = self.service.search(data) return jsonify({ 'status': 0, 'message': '成功检索到{}条数据'.format(count), 'data': results, 'total': count }) except Exception as error: return jsonify({ 'status': 500, 'message': '检索异常,请联系管理员!', 'data': str(error) }) @catch_exception def log(self): """ :return: """ if request.method == 'GET': data = request.values.to_dict() else: data = request.get_json() if 'id' not in data or not data['id']: return jsonify({ 'status': 400, 'message': '查询日志需要ID参数!', 'data': data }) try: result = self.service.log(data) if result is None: return jsonify({ 'status': 0, 'message': "日志不存在!", 'data': [] }) else: return jsonify({ 'status': 0, 'message': "日志不存在!", 'data': result }) except Exception as error: return jsonify({ 'status': 500, 'message': '检索异常,请联系管理员!', 'data': str(error) })
def __init__(self): super(ReportView, self).__init__() self.service = ReportService()