Exemplo n.º 1
0
 def getFile(self):
     ex.console.clear()
     global sheetNames, sheetName, fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, book, sheet1, fileRes, column, fname, path, file, exampleList
     try:
         reportDate = time.strftime("%Y%m%d", time.localtime())
         self.example.clear()
         '''
         @支持两种格式的excel文件
         '''
         fname, _ = QFileDialog.getOpenFileName(self, 'open file', '/',
                                                "files (*.xls *.xlsx)")
         self.fileName.setToolTip(fname)
         '''
         @获取文件的路径和名称,供其他方法使用
         '''
         self.fileName.setText(fname)
         path, file = self.getPath(fname)
         '''
         @初始化日志、配置文件
         '''
         fileData, email, userParams, userParamsValue = self.initConfig(
             path)
         initLog(path)
         '''
         @读取文件内容
         '''
         data = readExcel(path + '/' + file)
         sheetNames = getSheetNames(file, data)
         book, sheet1, fileRes = self.createReport(reportDate, path, file,
                                                   data, sheetNames)
         self.qSheetName.clear()
         self.example.clear()
         fname = self.fileName.text()
         '''
         @填充页签下拉列表
         '''
         for i in range(0, len(sheetNames) + 1):
             if i == 0:
                 self.qSheetName.addItem('全部')
             else:
                 self.qSheetName.addItem(str(sheetNames[i - 1]))
         '''
         @如果未选择文件,页签下拉列表置空
         '''
         if fname == '请选择文件' or fname == '':
             self.qSheetName.clear()
             self.console.clear()
         self.successNum.setText('0')
         self.failNum.setText('0')
         self.skipNum.setText('0')
     except Exception as e:
         print(e)
         self.qSheetName.clear()
Exemplo n.º 2
0
    def getSheet(self, reportDate, path, sheetName, file, data):
        try:
            data = readExcel(path + '/' + file)
        except Exception as e:
            self.console.append("<font color=\"#FF0000\">" + str(e) +
                                "</font> ")
            self.console.append("<font color=\"#000000\"></font> ")
        try:
            '''
            #生测试报告,历史报告移动到history中
            '''
            if file[-4:] == '.xls':
                sheet = data.sheet_by_name(sheetName)
                nrows = sheet.nrows
                ncols = sheet.ncols
            elif file[-5:] == '.xlsx':
                sheet = data.get_sheet_by_name(sheetName)
                nrows = sheet.max_row
                ncols = sheet.max_column
            try:
                isExists = os.path.exists(path + '/result/history')
                if not isExists:
                    os.makedirs(path + '/result/history')
                fileList = os.listdir(path + '/result/')
                for i in range(0, len(fileList)):
                    if str(reportDate) not in str(
                            fileList[i]) and str('report') in str(fileList[i]):
                        try:
                            shutil.move(path + '/result/' + str(fileList[i]),
                                        path + '/result/history')
                        except Exception as e:
                            print(e)
                return sheet, nrows, ncols
            except Exception as e:
                print(e)

        except Exception as e:
            print(e)
Exemplo n.º 3
0
 def reloadSheet(self):
     try:
         fname = self.fileName.text()
         if fname == '请选择文件' or fname == '':
             pass
         else:
             self.qSheetName.clear()
             '''
             @读取文件内容
             '''
             data = readExcel(path + '/' + file)
             sheetNames = getSheetNames(file, data)
             '''
             @填充页签下拉列表
             '''
             for i in range(0, len(sheetNames) + 1):
                 if i == 0:
                     self.qSheetName.addItem('全部')
                 else:
                     self.qSheetName.addItem(str(sheetNames[i - 1]))
             self.qSheetName.setCurrentIndex(0)
     except Exception as e:
         print(e)
Exemplo n.º 4
0
    def taskJob(self):
        dict = {}
        htmlReportName = ''
        try:
            ex.status1 = 0  #success
            ex.status2 = 0  #fail
            ex.status3 = 0  #skip
            ex.allRows = 0
            fname = ex.fileName.text()
            reportDate = time.strftime("%Y%m%d", time.localtime())
            if fname == '请选择文件' or fname == '':
                pass
            else:
                ex.console.append("<font color=\"#000000\">" + '定时任务开始:' +
                                  "</font>")
                now_time = datetime.datetime.now()
                startTime = datetime.datetime.strptime(
                    str(now_time)[:-7], '%Y-%m-%d %H:%M:%S')
                ex.successNum.setText('0')
                ex.failNum.setText('0')
                ex.skipNum.setText('0')
                ex.result.setText('0/0')
                ex.abort.setEnabled(False)
                ex.debug.setEnabled(False)
                ex.file.setEnabled(False)
                ex.analyJSON.setEnabled(False)
                ex.dtailReport.setEnabled(False)
                ex.html.setEnabled(False)
                ex.dtailLog.setEnabled(False)
                ex.qSheetName.setEnabled(False)
                ex.refresh.setEnabled(False)
                data = readExcel(path + '/' + file)
                book, sheet1, fileRes = ex.createReport(
                    reportDate, path, file, data, sheetNames)
                allRpt = ''
                try:
                    '''
                    @定时任务只支持执行全部用例,
                    '''
                    model = '普通' if ex.model1.isChecked() else '简洁'
                    for i in range(0, len(sheetNames)):
                        fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, column = ex.init(
                            reportDate, path, file, sheetNames[i])
                        rpt = ex.verTemp(file, sheetNames[i], sheet, ncols,
                                         book, sheet1[i], fileRes, column)
                        if rpt == '':
                            noRuns = 0
                            IterationCol = findStr(file, sheet, ncols,
                                                   'Iteration')
                            for i in range(3, nrows + 1):
                                if str(
                                        getValue(file, sheet, i - 1,
                                                 IterationCol)).upper() == '0':
                                    noRuns = noRuns + 1
                            ex.allRows = ex.allRows + nrows - 2 - noRuns
                        allRpt = allRpt + str(rpt)
                    '''
                    @所有页签的模板校验通过
                    '''
                    if allRpt == '':
                        testResult = []
                        for i in range(0, len(sheetNames)):
                            fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, column = self.init(
                                reportDate, path, file, sheetNames[i])
                            dict, tr = ex.run(file, model, '', sheetNames[i],
                                              userParams, userParamsValue,
                                              sheet, nrows, book, sheet1[i],
                                              fileRes, column, ex.allRows)
                            testResult = testResult + tr
                except Exception as e:
                    print(e)
                    ex.console.append("<font color=\"#FF0000\">" + '定时任务执行失败' +
                                      "</font> ")
                    ex.console.append("<font color=\"#000000\"></font>")

                now_time = datetime.datetime.now()
                endTime = datetime.datetime.strptime(
                    str(now_time)[:-7], '%Y-%m-%d %H:%M:%S')
                duration = datetime.datetime.strptime(str(endTime - startTime),
                                                      '%H:%M:%S')
                dd = str(duration)[11:].split(':')
                for i in range(0, len(dd)):
                    if dd[i][0:1] == str(0):
                        dd[i] = dd[i][1:]
                duration = dd[0] + '小时 ' + dd[1] + '分 ' + dd[2] + '秒'
                taskName = file[:-4] if file[-4:] == '.xls' else file[:-5]
                '''
                @测试结果存到字典中,用于html测试报告
                '''
                dict['testName'] = taskName  #项目名称
                dict['beginTime'] = str(startTime)  #开始时间
                dict['totalTime'] = duration  #运行时长
                dict['testResult'] = testResult  #结果集
                htmlReportName = ex.createHTMLReport(reportDate, dict, file,
                                                     path)
                ex.sendEmail(htmlReportName)
                ex.console.append("<font color=\"#000000\"></font>")
                ex.console.append("<font color=\"#000000\">" + '定时任务执行成功' +
                                  "</font>")
                scheduler.shutdown()
                ex.status1 = 0  #success
                ex.status2 = 0  #fail
                ex.status3 = 0  #skip
                ex.allRows = 0
        except Exception as e:
            print(e)
            ex.status1 = 0  #success
            ex.status2 = 0  #fail
            ex.status3 = 0  #skip
            ex.allRows = 0
        ex.task.setEnabled(True)
        ex.abort.setEnabled(True)
        ex.taskTime.setEnabled(True)
        ex.debug.setEnabled(True)
        ex.file.setEnabled(True)
        ex.analyJSON.setEnabled(True)
        ex.dtailReport.setEnabled(True)
        ex.html.setEnabled(True)
        ex.dtailLog.setEnabled(True)
        ex.qSheetName.setEnabled(True)
        ex.refresh.setEnabled(True)
Exemplo n.º 5
0
    def run(self):
        dict = {}
        htmlReportName = ''
        ex.runFlag = True
        cText = ex.debug.text()
        if cText == '停止':
            ex.successNum.setText('0')
            ex.failNum.setText('0')
            ex.skipNum.setText('0')
            ex.status1 = 0  #success
            ex.status2 = 0  #fail
            ex.status3 = 0  #skip
            ex.allRows = 0

        try:
            reportDate = time.strftime("%Y%m%d", time.localtime())
            model = '普通' if ex.model1.isChecked() else '简洁'
            now_time = datetime.datetime.now()
            startTime = datetime.datetime.strptime(
                str(now_time)[:-7], '%Y-%m-%d %H:%M:%S')
            ex.successNum.setText('0')
            ex.failNum.setText('0')
            ex.skipNum.setText('0')
            ex.console.append("<font color=\"#000000\"></font>")
            text = ex.result.text()
            ex.result.setText('0' + text[text.index('/'):])
            ex.task.setEnabled(False)
            ex.abort.setEnabled(False)
            ex.file.setEnabled(False)
            ex.analyJSON.setEnabled(False)
            ex.dtailReport.setEnabled(False)
            ex.html.setEnabled(False)
            ex.dtailLog.setEnabled(False)
            ex.qSheetName.setEnabled(False)
            ex.refresh.setEnabled(False)
            data = readExcel(path + '/' + file)
            fileData, email, userParams, userParamsValue = ex.initConfig(path)
            book, sheet1, fileRes = ex.createReport(reportDate, path, file,
                                                    data, sheetNames)
            sheetValue = ex.qSheetName.currentText()
            allRpt = ''
            testResult = []
            '''
            @全量
            '''
            if sheetValue == '全部' and ex.qSheetName.currentIndex() == 0:
                ex.console.append("<font color=\"#000000\"></font>")
                for i in range(0, len(sheetNames)):
                    fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, column = ex.init(
                        reportDate, path, file, sheetNames[i])
                    rpt = ex.verTemp(file, sheetNames[i], sheet, ncols, book,
                                     sheet1[i], fileRes, column)
                    '''
                    @模板校验通过
                    '''
                    if rpt == '':
                        noRuns = 0
                        '''
                        @找出迭代次数为0(不执行)的用例
                        '''
                        for i in range(3, nrows + 1):
                            if str(getValue(file, sheet, i - 1,
                                            column[24])).upper() == '0':
                                noRuns = noRuns + 1
                        '''
                        @全部用例数为各页签的用例数相加减去不执行的用例数
                        '''
                        ex.allRows = ex.allRows + nrows - 2 - noRuns  #全部用例数
                    allRpt = allRpt + str(rpt)
                '''
                @模板全部校验通过
                '''
                if allRpt == '':
                    for i in range(0, len(sheetNames)):
                        '''
                        @初始化页签
                        '''
                        fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, column = ex.init(
                            reportDate, path, file, sheetNames[i])
                        '''
                        @执行该页签中的用例
                        '''
                        dict, tr = ex.run(file, model, '', sheetNames[i],
                                          userParams, userParamsValue, sheet,
                                          nrows, book, sheet1[i], fileRes,
                                          column, ex.allRows)
                        '''
                        @拼接结果集
                        '''
                        testResult = testResult + tr
            else:  #单个或多个
                for i in range(0, len(sheetNames)):
                    if sheetValue == sheetNames[i]:
                        sheet1 = sheet1[i]
                        break
                fileData, email, userParams, userParamsValue, data, sheet, nrows, ncols, column = ex.init(
                    reportDate, path, file, sheetName)
                rpt = ex.verTemp(file, sheetName, sheet, ncols, book, sheet1,
                                 fileRes, column)
                '''
                @模板校验通过才进行之后的操作
                '''
                if rpt == "":
                    exa = ex.example.currentText()
                    en = []
                    '''
                    @未选中任何用例则执行该页签的全部用例
                    '''
                    if exa == []:
                        for i in range(3, nrows + 1):
                            '''
                            @迭代次数为0表示此用例不执行
                            '''
                            if str(getValue(file, sheet, i - 1,
                                            column[24])) != '0':
                                exa.append(i)
                    else:
                        '''
                        @取选中的用例列表                        
                        '''
                        exa = exa.replace("'", '').replace('(', '').replace(
                            ')', '').split(',')
                        '''
                        @是否存在大于当前页签行数的接口号(删除用例引起)
                        '''
                        en = [int(item) for item in exa if int(item) > nrows]
                    '''
                    @如果当前选中的用例(序列号大于nrows)已被删除,则提示
                    '''
                    if en != []:
                        ex.console.append("<font color=\"#FF0000\">" + '用例' +
                                          str(en) + "不存在" + "</font>")
                    else:
                        for item in exa:
                            if str(
                                    getValue(
                                        file, sheet,
                                        int(item) - 1,
                                        column[24])) != '0':  #不执行迭代次数为0的用例
                                dict, tr = ex.run(file, model, int(item),
                                                  sheetName, userParams,
                                                  userParamsValue, sheet,
                                                  nrows, book, sheet1, fileRes,
                                                  column, len(exa))
                                testResult = testResult + tr
            '''
            @格式化html报告中的运行时间和时长
            '''
            now_time = datetime.datetime.now()
            endTime = datetime.datetime.strptime(
                str(now_time)[:-7], '%Y-%m-%d %H:%M:%S')
            duration = datetime.datetime.strptime(str(endTime - startTime),
                                                  '%H:%M:%S')
            dd = str(duration)[11:].split(':')
            for item in dd:
                if item[0:1] == '0':
                    item = item[1:]
            duration = dd[0] + '小时 ' + dd[1] + '分 ' + dd[2] + '秒'
            taskName = file[:-4] if file[-4:] == '.xls' else file[:-5]
            '''
            @测试结果存到字典中,用于html测试报告
            '''
            dict['testName'] = taskName  #项目名称
            dict['beginTime'] = str(startTime)  #开始时间
            dict['totalTime'] = duration  #运行时长
            dict['testResult'] = testResult  #结果集
            htmlReportName = ex.createHTMLReport(reportDate, dict, file, path)
            ex.sendEmail(htmlReportName)
            ex.status1 = 0  #success
            ex.status2 = 0  #fail
            ex.status3 = 0  #skip
            ex.allRows = 0
            ex.debug.setText('开始')
        except Exception as e:
            print(e)
            ex.status1 = 0  #success
            ex.status2 = 0  #fail
            ex.status3 = 0  #skip
            ex.allRows = 0
        ex.task.setEnabled(True)
        ex.abort.setEnabled(True)
        ex.file.setEnabled(True)
        ex.analyJSON.setEnabled(True)
        ex.dtailReport.setEnabled(True)
        ex.html.setEnabled(True)
        ex.dtailLog.setEnabled(True)
        ex.qSheetName.setEnabled(True)
        ex.refresh.setEnabled(True)
        ex.debug.setText('开始')