unittest 统计测试执行case总数,成功数量,失败数量,输出至文件,生成一个简易的html报告带饼图

这是一个Python的单元测试框架的示例代码,主要用于执行测试用例并生成测试报告。其中,通过unittest模块创建主测试类MainTestCase,并加载其他文件中的测试用例,统计用例的执行结果并将结果写入文件,最后生成一个简单的测试报告html页面。

在执行测试时,可以根据需要添加更多的测试文件和测试用例,在自定义CustomTextTestResult类中重写addSuccess和addFailure方法,获取用例的执行结果,并在统计成功和失败的用例数量时进行累加。将用例执行成功和失败的信息分别保存到success_cases和failure_cases列表中,最后将结果写入文件。

在生成测试报告时,利用HTML和CSS语言生成一个表格结构的页面,并将测试结果以文本和图表的形式展示出来。在页面中使用了一些JavaScript代码来计算成功和失败的百分比,并将其以饼状图的形式呈现出来。最终,将html代码写入到文件中,生成测试报告。

main.py

import unittest
import time


class MyTestcase(unittest.TestCase):

    def test_1(self):
        print('main_test_1')
        self.assertEqual(2 - 2, 4)

    def test_2(self):
        print('main_test_2')
        self.assertEqual(2 - 2, 4)

    def test_3(self):
        print('test_3')
        time.sleep(2)
        self.assertEqual(2 + 2, 4)

    def test_4(self):
        print('test_4')
        self.assertEqual(2 - 2, 4)

    def test_5(self):
        print('test_5')
        self.assertEqual(2 + 2, 4)

    def test_6(self):
        print('test_6')
        self.assertEqual(2 + 2, 4)

    def test_7(self):
        print('test_7')
        self.assertEqual(2 + 2, 4)

    def test_8(self):
        print('test_8')
        self.assertEqual(2 + 3, 4)


 Testcase2.py

import unittest

class TestcaseGo(unittest.TestCase):

    def test_1(self):
        print('TestcaseGo_test_1')
        self.assertEqual(2 - 2, 4)

    def test_2(self):
        print('TestcaseGo_test_2')
        self.assertEqual(2 - 2, 4)

    def test_3(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_4(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_5(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_6(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_7(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_8(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_9(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_10(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_11(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)


    def test_12(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_13(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_14(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_15(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_16(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_17(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_18(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_19(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_20(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_21(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_22(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_23(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

    def test_24(self):
        print('TestcaseGo_test_3')
        self.assertEqual(2 + 2, 4)

 run.py

import unittest
import datetime


# 创建主测试类
class MainTestCase(unittest.TestCase):
    pass


if __name__ == '__main__':

    # 统计测试用例执行开始时间
    today1 = datetime.date.today()
    current_time1 = datetime.datetime.now().time()
    # print("执行开始时间:" + str(today1) +" "+ str(current_time1))

    # 创建测试加载器
    loader = unittest.TestLoader()

    # 加载其他Python文件中的测试用例并添加到主测试类中
    test_cases = [
        loader.loadTestsFromName('main.MyTestcase'),
        loader.loadTestsFromName('Testcase2.TestcaseGo')
        # 添加更多的测试文件和测试用例
    ]
    suite = unittest.TestSuite(test_cases)
    suite.addTests(loader.loadTestsFromTestCase(MainTestCase))

    # 计算总用例数量
    total_count = suite.countTestCases()

    # 定义全局的成功、失败和总数计数变量
    success_count = 0
    failure_count = 0

    # 保存执行成功的用例和执行失败的用例
    success_cases = []
    failure_cases = []


    # 重写run()方法,获取用例的执行结果
    class CustomTextTestResult(unittest.TextTestResult):
        def addSuccess(self, test):
            global success_count
            success_count += 1
            success_cases.append(str(test))
            super().addSuccess(test)

        def addFailure(self, test, err):
            global failure_count
            failure_count += 1
            failure_cases.append(str(test))
            super().addFailure(test, err)


    # 创建测试运行器并运行测试
    runner = unittest.TextTestRunner(verbosity=2, resultclass=CustomTextTestResult)
    result = runner.run(suite)

    #  将结果写入文件,指定编码格式为UTF-8
    with open("test_result.txt", "w", encoding="utf-8") as file:
        file.write(f"成功的用例数量: {success_count}\n")
        file.write(f"失败的用例数量: {failure_count}\n")
        file.write(f"总用例数量: {total_count}\n")

        file.write("\n执行成功的用例:\n")
        for case in success_cases:
            file.write(case + "\n")

        file.write("\n执行失败的用例:\n")
        for case in failure_cases:
            file.write(case + "\n")

    print("测试结果已写入文件 test_result.txt")


    # print(total_count)
    # print(success_count)
    # print(failure_count)
    # print(success_cases)
    # print(failure_cases)

    # 生成一个简单的测试报告html页面
    def html_report(today1, current_time1, total_count, success_count, failure_count, success_cases, failure_cases):

        # 用例总数量、成功数量、失败数量、百分比
        Success_Rate = (success_count / total_count) * 100
        failure_Rate = (failure_count / total_count) * 100
        # print(int(Success_Rate))
        # print(int(failure_Rate))
        Success_Rate_text = str(int(Success_Rate)) + '%'
        failure_Rate_text = str(int(failure_Rate)) + '%'
        # print(Success_Rate_text)
        # print(failure_Rate_text)
        Success_Rate_text_b = '{'+str(Success_Rate)+'}%'

        # 拼接元素
        # text_content1 = "\n".join(success_cases)
        # text_content2 = "\n".join(failure_cases)

        test_cases_with_br = [f"{case}
" for case in success_cases] # 将带有
标签的元素连接成一个字符串 text_content1 = "".join(test_cases_with_br) test_cases_with_br = [f"{case}
" for case in failure_cases] # 将带有
标签的元素连接成一个字符串 text_content2 = "".join(test_cases_with_br) text_content1 = text_content1 text_content2 = text_content2 # 统计测试用例执行时间 today2 = datetime.date.today() current_time2 = datetime.datetime.now().time() # print("执行结束时间:" + str(today2) + str(current_time2)) time_diff = datetime.datetime.combine(datetime.date.min, current_time2) - datetime.datetime.combine( datetime.date.min, current_time1) strat_time = str(today1) + " " + str(current_time1) over_time = str(today2) + " " + str(current_time2) # 测试报告html源码 report = """ 表格示例

成功的用例数量 成功的用例占比 失败的用例数量 失败的用例占比 总用例数量
{success_count} {Success_Rate_text} {failure_count} {failure_Rate_text} {total_count}

执行开始时间 执行结束时间 执行总耗时
{strat_time} {over_time} {time_diff}


成功
失败

执行成功的用例 执行失败的用例
{text_content1} {text_content2}


""".format(time_diff=time_diff, strat_time=strat_time, over_time=over_time, total_count=total_count, success_count=success_count, Success_Rate_text=Success_Rate_text, failure_count=failure_count, failure_Rate_text=failure_Rate_text, text_content1=text_content1, text_content2=text_content2) \ + \ """ """.format(Success_Rate_text=Success_Rate_text_b) + """ """ # ${successPercentage}% # $81% print(report) with open("html_result.html", "w", encoding="utf-8") as file: file.write(report) print("测试报告生成成功!") html_report(today1, current_time1, total_count, success_count, failure_count, success_cases, failure_cases) # html_report(total_count, success_count, failure_count, success_cases, failure_cases)

unittest 统计测试执行case总数,成功数量,失败数量,输出至文件,生成一个简易的html报告带饼图_第1张图片

unittest 统计测试执行case总数,成功数量,失败数量,输出至文件,生成一个简易的html报告带饼图_第2张图片

你可能感兴趣的:(#,unittest单元测试框架,python)