栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

自动化测试之:混合驱动加unittest

自动化测试之:混合驱动加unittest

from base.base_page import basePage
import config.keyword
from test import test_support
import HTMLTestRunner
# from testinit import *
import os
import sys
import time
import unittest
import HTMLTestRunner
import unittest
from testinit.my_unit import MyUnit
from BeautifulReport import BeautifulReport
from PIL import ImageGrab
sys.path.append('C:\Users\dell\AppData\Local\Programs\Python\Python39\Lib\BeautifulReport\template')


age = 10
#继承unittest.TestCase类,ExecutionEngine就是TestCase的子类
class ExecutionEngine(MyUnit):
    def save_img(self,test_method):
        root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).replace('\','/')
        img_path, = root_dir + '/testinit'
        filepath = '{}/{}.png'.format(img_path, test_method)
        ImageGrab.grab().save(filepath)
        print("已执行sshot")

    def get_time_stamp(self):
        ct = time.time()
        local_time = time.localtime(ct)
        data_head = time.strftime("%Y_%m_%d_%H_%M_%S", local_time)
        data_secs = (ct - int(ct)) * 1000
        time_stamp = "%s_%03d" % (data_head, data_secs)
        return time_stamp

    @classmethod
    def setUpClass(cls):
        cls.filepath = r"D:pythonProjectpythonProjectdataEnginedata.xls"
        print('start')

    @unittest.skipIf(age>9,reason='大于九岁')
    def test_01_hah(self):
        print("然然")

    @BeautifulReport.add_test_img('test_02_hah')
    def test_02_hah(self):
        print("薇薇")

    @BeautifulReport.add_test_img('test_action_{}'.format(time.time()))
    def action(self,*txt):
        """测试demo"""
        exeKeyword =config.keyword.Actionkeywords()
        base = basePage()
        case_id = txt[1]
        print(case_id)
        casedata = base.getTabledata(self.filepath, "Test Cases")
        stepdata = base.getTabledata(self.filepath, "Test Steps")
        for j in stepdata:
            if txt[0] == j[0]:
                print("*************Tlog:【步骤】",j[1],"|【描述】",j[2],'|【当前关键字】',j[3])
                if j[3] == "starttest":
                    exeKeyword.starttest()

                elif j[3] == "click_picture":
                    keyword,value1,value2= j[3],j[4],j[5]
                    command = 'exeKeyword.{0}(base.{1},"{2}")'.format(keyword,value1,value2)
                    eval(command)
                    # raise Exception("自定义异常")

                elif j[3] == "input_text":
                    keyword, value1 = j[3], j[4]
                    command = 'exeKeyword.{0}({1})'.format(keyword, value1)
                    eval(command)


    def getTestFunc(*txt):
        def func(self):
            self.action(*txt)
        return func

    # 脚本退出
    @classmethod
    def tearDownClass(cls):
        print("End")


def __generateTestCases():
    base = basePage()
    casedata = base.getTabledata(r"D:pythonProjectpythonProjectdataEnginedata.xls", "Test Cases")
    print(casedata)
    for i in casedata:
        TCid = i[0]
        if i[3] == "Y":
            print ("【Run】"+i[1]+":")
            print (" + -"*16)
            table = base.getTabledata(r"D:pythonProjectpythonProjectdataEnginedata.xls", "case data")
            for txt in table:
                if (txt[2] == "Y") & (txt[0] == TCid):
                    print ("用例描述",txt)
                    setattr(ExecutionEngine,'test_%s_%s' % (txt[0], txt[1]), ExecutionEngine.getTestFunc(*txt))

__generateTestCases()

def test_main():
	test_support.run_unittest(ExecutionEngine)



# # __generateTestCases()
if __name__ == "__main__":
    print(sys.path)
    # print(os.path.abspath("./testinit"))
    Testcases = unittest.defaultTestLoader.discover("./",pattern ='tdriver*.py')
    # print("suite如下:","n",Testcases,"n","当前开始执行suite集合用例")
    # unittest.main(defaultTest='Testcases',verbosity=2)
    # runner = unittest.TextTestRunner()文本生成测试结果
    # runner.run(Testcases)
    #第三方HTML文本生成器
    # file = open(r"D:pythonProjectpythonProjectreportstest.html",'wb')#没有文件时自动创建
    # runner = HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=1, title="Pair-v2.6-1", description="Pair2.6测试用例执行情况如下:测试负责人:罗依然:")
    # print("runner ....ok")
    result = BeautifulReport(Testcases)
    result.report(filename='Pair2.6测试报告', description='VERSION =2.6.0-1 测试人:罗依然 ', log_path='.')
    # runner.run(Testcases)

 

if __name__ == "__main__":
    print(sys.path)
    # print(os.path.abspath("./testinit"))
    Testcases = unittest.defaultTestLoader.discover("./",pattern ='tdriver*.py')   
result = BeautifulReport(Testcases)
    result.report(filename='Pair2.6测试报告', description='VERSION =2.6.0-1 测试人:罗依然 ', log_path='.')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号