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

import os
import unittest

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

# 第二种方法:通过loader来加载用例-通过模块加载用例
from class1228_unittest_loader.test_cases import test_setup
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromModule(test_setup))
# 创建存放测试报告的文件夹-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)
原文地址:https://www.cnblogs.com/benben-wu/p/12131498.html