使用unittest自动生成用例,并生成BeautifulReport

import unittest
from Logic.ComputationalLogic import ComputionalLogic
from BeautifulReport import BeautifulReport
from Common import Basic

class Case(unittest.TestCase):
    global comp
    comp = ComputionalLogic()
    
    def annual_comp(self,apidata):
        """**校验"""
        back = comp.computation_flow(apidata)
        self.assertIn(back, [0,None])
      
def demo(apidata):    #使用闭包,把函数作为返回参数被其他调用
    def tool(self):
        Case.annual_comp(self,apidata)   #每条用例初始化Case类
    setattr(tool, '__doc__', u'%s' % str(apidata))  #替换描述为用例名
    return tool
        
def testall(apidata):   
    nameList = []
    for i in range(len(apidata)):
        name = 'test_' + str(i + 1)    #自增生成用例名
        setattr(Case, name, demo(apidata[i]))    #调用闭包,生成用例
        nameList.append(name)
    return nameList

def suite(Apidata):  
    nameList = testall(Apidata)
    suites = unittest.TestSuite()
    for i in nameList:
        suites.addTest(Case(i))
    return suites
        
if __name__ == '__main__':
    Basic.clearLogging()
    product_data = Basic.GetDataFromYaml('..//Config//Product.yaml')
    case_list = list(product_data.keys())    #把yaml文件中每个键加载为suit
    print(case_list)
    suit = suite(case_list) 
    result = BeautifulReport(suit)
    result.report(filename='测试报告', description='*****报告', log_path='D:/workspace/DeductCompare_New/Report')

相当于往unnittest塞数据:

Case.name = "test_自增"

Case.__doc__ =  "产品名+资方+产品类型"

Case.fun = comp.computation_flow

 

参考原文:https://www.cnblogs.com/fengyca/p/8434630.html

BeautifulReport下载地址: https://github.com/TesterlifeRaymond

感谢上面两位作者

转载于:https://www.cnblogs.com/carlvine/articles/9682197.html

你可能感兴趣的:(使用unittest自动生成用例,并生成BeautifulReport)