python中unittest单元测试框架-加载测试用例、运行测试用例、生成测试报告-通过测试类名加载用例

import os
import unittest

# 创建suite对象
suite = unittest.TestSuite()

# 第三种方法:通过loader来加载用例-通过测试类名加载用例
from class1228_unittest_loader.test_cases.test_setup import *
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestSetUp))

# 创建存放测试报告的文件夹-report
dir = os.path.dirname(os.path.abspath(__file__))
report_path = os.path.join(dir, 'report')
if not os.path.exists(report_path):
    os.mkdir(report_path)

file_path = os.path.join(report_path, 'test_result.txt')

with open(file_path, "w") as f:
    # 初始化运行器, 是以普通文本生成测试报告 TextTestRunner
    runner = unittest.TextTestRunner(f, verbosity=2)
    # 运行测试用例
    runner.run(suite)

猜你喜欢

转载自www.cnblogs.com/benben-wu/p/12131541.html