Exemple #1
0
 def DrawPie(self, result):
     """
     绘制饼图用pie
     :return:
     """
     labels = 'OK', 'F', 'E'
     fracs = [
         result.success_count, result.failure_count, result.error_count
     ]
     colors = ['green', 'orange', 'red']
     explode = [0, 0, 0]  # 0.1 凸出这部分,
     plt.axes(
         aspect=1)  # set this , Figure is round, otherwise it is an ellipse
     # autopct ,show percet
     plt.pie(x=fracs,
             colors=colors,
             labels=labels,
             explode=explode,
             autopct='%3.1f %%',
             shadow=True,
             labeldistance=1.1,
             startangle=90,
             pctdistance=0.6)
     # plt.show()
     # 显示图例
     plt.legend()
     #设置饼图截图路径
     gts = GetTimeStr()
     tStr = gts.getTimeStr()  #获取当前时间串
     currentny = gts.getTimeStrNY()  #获取当前时间的年月
     firedir = r'%s/media/report/%s/pies/' % (os.path.dirname(
         os.path.dirname(os.path.dirname(
             os.path.abspath(__file__)))), currentny)
     gts.createdir(firedir)
     logPath = firedir
     imgPath = os.path.join(logPath, "%s_pie.png" % tStr)
     #保存饼图
     plt.savefig(imgPath)
     #显示饼图路径:
     showimgPath = r'%s/media/report/%s/pies/%s_pie.png' % (
         DJANGO_SERVER_YUMING, currentny, tStr)
     #返回饼图路径
     return showimgPath
Exemple #2
0
    def runAllTest(self, testproject=None, testmodule=None):

        if testproject == None:
            testproject = u"测试项目"
        else:
            testproject = testproject
        if testmodule == None:
            testmodule = u"测试模块"
        else:
            testmodule = testmodule

        #将用例组件成数组
        alltestnames = caselist()
        suite = unittest.TestSuite()
        for testpy in alltestnames:
            try:
                suite.addTest(
                    unittest.defaultTestLoader.loadTestsFromName(
                        testpy))  #默认加载所有用例
            except Exception:
                print('ERROR: Skipping tests from "%s".' % testpy)
                try:
                    __import__(test)
                except ImportError:
                    print('Could not import the "%s" test module.' % testpy)
                else:
                    print('Could not load the "%s" test suite.' % testpy)
                from traceback import print_exc
                print_exc()
        self.outPutMyLog('Running the tests...')
        # print('Running the tests...')
        gettime = GetTimeStr()
        reporttimestr = gettime.getTimeStr()  #获取当前时间
        currentny = gettime.getTimeStrNY()  #获取当前时间的年月
        reportdir = "%s/media/report/%s" % (str(
            os.path.dirname(
                os.path.dirname(os.path.dirname(
                    os.path.abspath(__file__))))), currentny)
        gettime.createdir(reportdir)  #创建报告目录
        reportname = "report/%s/%s_report.html" % (currentny, reporttimestr)
        filename = '%s/media/%s' % (str(
            os.path.dirname(
                os.path.dirname(os.path.dirname(
                    os.path.abspath(__file__))))), reportname)
        fp = open(filename, 'wb')
        self.outPutMyLog('The report path:%s' % filename)

        # 定义测试报告
        runner = HTMLTestRunner.HTMLTestRunner(
            stream=fp,
            title=u'%s_%s 自动化测试_测试报告' % (testproject, testmodule),
            description=u'用例执行情况:',
            verbosity=2)  #verbosity=2,输出测试用例中打印的信息
        runresult = runner.run(suite)
        fp.close()

        #保存报告到数据库
        from reportdatas.models import Report
        reportrd = Report()  # 数据库的对象等于Report,实例化
        reportrd.testproject = testproject
        reportrd.testmodule = testmodule
        reportrd.reportname = reporttimestr
        print(reportname)
        reportrd.reportfile = reportname
        reportrd.save()
        from wanwenyc.settings import DJANGO_SERVER_YUMING
        reportnew = Report.objects.all().order_by('-id')[:1][0]  #获取最新报告
        reporturl = "%s/media/%s" % (DJANGO_SERVER_YUMING, reportnew.reportfile
                                     )  #最新报告访问网址

        # 发送report至邮箱
        emailtitle = u'%s_%s 自动化测试_测试报告' % (testproject, testmodule)
        send_e = SendEmail()
        send_e.run_send(runresult.success_count,
                        runresult.failure_count,
                        runresult.error_count,
                        filename,
                        emailtitle=emailtitle,
                        reporturl=reporturl)