예제 #1
0
    def run(self):
        self.outPutMyLog('---------------------------')
        # print('---------------------------')
        stdout_backup = sys.stdout
        gettime = GetTimeStr()
        timestr = gettime.getTimeStr()
        # define the log file that receives your log info
        logpath = "%s/log/%s_message.txt" % (str(
            os.path.dirname(os.path.dirname(
                os.path.abspath(__file__)))), timestr)
        log_file = open(logpath, "w", encoding="utf-8")
        self.outPutMyLog('Now all print info will be written to message.log')
        # print("Now all print info will be written to message.log")
        # redirect print output to log file
        sys.stdout = log_file
        self.outPutMyLog('----------开始打印日志-----------------\n')
        # print('----------开始打印日志-----------------\n')

        # any command line that you will execute
        self.runAllTest()
        self.outPutMyLog('\n----------日志打印结束-----------------')
        # print('\n----------日志打印结束-----------------')
        log_file.close()
        # restore the output to initial pattern
        sys.stdout = stdout_backup
        self.outPutMyLog('Now this will be presented on screen')
        # print("Now this will be presented on screen")
        # 发送log至邮箱
        send_e = SendEmail()
        send_e.send_main([1], [2], logpath)
    def runAllTest(self):
        #将用例组件成数组
        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'python 自动化测试_测试报告',
            description=u'用例执行情况:',
            verbosity=2)  #verbosity=2,输出测试用例中打印的信息
        runner.run(suite)
        fp.close()

        #保存报告到数据库
        from reportrecord.models import Report
        reportrd = Report()  # 数据库的对象等于Report,实例化
        reportrd.reportname = reporttimestr
        print(reportname)
        reportrd.reportfile = reportname
        reportrd.save()

        # 发送report至邮箱
        send_e = SendEmail()
        send_e.send_main([1], [2], filename)
예제 #3
0
    def runAllTest(self):
        #将用例组件成数组
        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()
        filename = '%s/report/%s_report.html' % (str(
            os.path.dirname(os.path.dirname(
                os.path.abspath(__file__)))), gettime.getTimeStr())
        fp = open(filename, 'wb')
        self.outPutMyLog('The report path:%s' % filename)

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

        # 发送report至邮箱
        send_e = SendEmail()
        send_e.send_main([1], [2], filename)