testReport.py 6.3 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from app import db
from utils.mango import *
6
from utils import common
泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
7
from utils.helpers import ExcelHelper
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import xlsxwriter
from io import BytesIO
import ast
import datetime
from bson import ObjectId

test_report_summary_map = {
    'projectName': '测试项目',
    'testDomain': '测试环境',
    'testCount': '用例总数',
    'passCount': '通过数',
    'failedCount': '失败数',
    'passRate': '通过率',
    'comeFrom': '报告来源',
    'executorNickName': '执行人',
    'createAt': '生成时间',
    'totalTestSpendingTimeInSec': '总耗时/s'
}

# 使用 dic_get 定位数据
test_report_detail_map = {
    "['testBaseInfo', 'name']": '用例名称',
    "['testBaseInfo', 'requestMethod']": '请求方法',
    "['testBaseInfo', 'url']": '请求地址',
    "['testBaseInfo', 'headers']": '请求头',
    "['testBaseInfo', 'cookies']": '请求Cookie',
    "['testBaseInfo', 'presendParams']": '请求参数',
泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
35
    "['testBaseInfo', 'curl']": '复现 curl',
36 37 38 39 40 41 42 43
    "['testBaseInfo', 'checkHttpCode']": '状态码校验',
    "['responseHttpStatusCode']": '实际状态码',
    "['testBaseInfo', 'checkResponseData']": '数据校验',
    "['testBaseInfo', 'checkResponseNumber']": '数值校验',
    "['testBaseInfo', 'checkResponseSimilarity']": '相似度校验',
    "['responseData']": '实际数据',
    "['testConclusion']": '测试结论',
    "['testStartTime']": '测试开始时间',
44
    "['testBaseInfo', 'checkResponseTime']": '耗时校验/s',
45 46
    "['spendingTimeInSec']": '测试耗时/s',
}
47 48 49 50 51 52 53 54 55 56 57 58 59 60


# 类名定义 collection
class TestReport(Model):

    class Meta:
        database = db
        collection = 'testReport'


    # 字段
    _id = ObjectIdField()
    isDeleted = BooleanField(field_name='isDeleted', default=False)
    projectId = ObjectIdField()
61 62
    projectName = StringField()
    testDomain = StringField()
63 64
    createAt = DateField()
    lastUpdateTime = DateField()
65
    totalTestSpendingTimeInSec = FloatField()
66 67 68 69 70 71 72 73 74
    testCount = IntField()
    passCount = IntField()
    failedCount = IntField()
    passRate = StringField()
    testDetail = ArrayField()
    comeFrom = StringField()
    executorNickName = StringField()
    cronId = StringField()

75 76 77 78 79 80 81 82 83
    @classmethod
    def get_test_report_excel_bytes_io(cls, report_id):
        test_report = cls.find_one({'_id': ObjectId(report_id)})
        test_report = common.format_response_in_dic(test_report)

        bytes_io = BytesIO()
        workbook = xlsxwriter.Workbook(bytes_io, {'in_memory': True})

        summary_sheet = workbook.add_worksheet(u'测试报告概览')
84
        summary_sheet.freeze_panes(1, 0)
85
        detail_sheet = workbook.add_worksheet(u'测试报告详情')
86
        detail_sheet.freeze_panes(1, 0)
87

泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
88 89 90 91 92 93 94 95
        # 设置测试报告表头 format

        header_style = workbook.add_format()
        header_style.set_bg_color("#00CCFF")
        header_style.set_color("#FFFFFF")
        header_style.set_bold()
        header_style.set_border()

96 97
        # 测试报告概览表头
        for index, value in enumerate(test_report_summary_map.values()):
泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
98 99 100 101 102
            summary_sheet.write(0, index, value, header_style)

        # 设置测试报告概览每列宽度
        [ExcelHelper.ExcelSheetHelperFunctions.set_column_auto_width(summary_sheet, i)
         for i in range(len(test_report_summary_map.values()))]
103 104 105 106 107 108 109 110 111

        # 测试报告概览数据
        for index, value in enumerate(test_report_summary_map.keys()):
            summary_sheet.write(1, index, str(test_report.get(value, '(暂无此数据)')))

        test_details = test_report['testDetail']

        # 测试报告详情表头
        for index, value in enumerate(test_report_detail_map.values()):
泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            detail_sheet.write(0, index, value, header_style)

        # 设置测试报告详情每列宽度
        [ExcelHelper.ExcelSheetHelperFunctions.set_column_auto_width(detail_sheet, i)
         for i in range(len(test_report_detail_map.values()))]

        test_result_pass_style = workbook.add_format()
        test_result_pass_style.set_bg_color("#00ff44")
        test_result_pass_style.set_color("#FFFFFF")
        test_result_pass_style.set_bold()
        # test_result_pass_style.set_border()

        test_result_failed_style = workbook.add_format()
        test_result_failed_style.set_bg_color("#ff0026")
        test_result_failed_style.set_color("#FFFFFF")
        test_result_failed_style.set_bold()
        # test_result_failed_style.set_border()

        failed_curl_style = workbook.add_format()
        failed_curl_style.set_bg_color("#ffee00")
        # failed_curl_style.set_color("#FFFFFF")
        failed_curl_style.set_bold()
        # failed_curl_style.set_border()
135 136 137 138 139

        # 测试报告详情数据
        for index, locator in enumerate(test_report_detail_map.keys()):
            locator = ast.literal_eval(locator)
            for col_index, detail in enumerate(test_details):
泰斯特Test's avatar
[feat]  
泰斯特Test 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                if 'testConclusion' in str(locator):
                    test_result = str(common.dict_get(detail, locator))
                    if '测试通过' in test_result:
                        detail_sheet.write(col_index + 1, index,
                                           test_result, test_result_pass_style)
                    else:
                        detail_sheet.write(col_index + 1, index,
                                           test_result, test_result_failed_style)
                elif 'curl' in str(locator):
                    test_result_status = str(common.dict_get(detail, ['status']))
                    if test_result_status == 'failed':
                        detail_sheet.write(col_index + 1, index,
                                           str(common.dict_get(detail, locator)), failed_curl_style)
                    else:
                        detail_sheet.write(col_index + 1, index, str(common.dict_get(detail, locator)))
                else:
156 157 158
                    pre_write_value = str(common.dict_get(detail, locator))
                    pre_write_value = '(空)' if pre_write_value == 'None' else pre_write_value
                    detail_sheet.write(col_index + 1, index, pre_write_value)
159 160 161 162 163 164 165

        workbook.close()

        bytes_io.seek(0)

        return bytes_io

166 167 168 169 170 171 172
    def __str__(self):
        return "createAt:{}"\
            .format(self.createAt)


if __name__ == '__main__':
    pass