# 导入测试类执行测试用例 import unittest from day_20191202.class_unittest import TestMathMethod, TestMulti # from day_20191202 import class_unittest # 为了运行模块中多个测试类 import HTMLTestRunner # ------------------------------ # 方法1 只执行部分用例 # suite = unittest.TestSuite() # 存储用例 # suite.addTest(TestMathMethod("test_add_two_zero")) # suite.addTest(TestMathMethod("test_add_two_positive")) # 添加一个用例,传入的参数是函数名 # # # # 执行存储的两个用例 # runner = unittest.TextTestRunner() # runner.run(suite) # ----------------------------------------------- # 方法2,TestLoader 按照从上到下的顺序执行测试类中的所有用例 # loader = unittest.TestLoader() # 加载用例 # suite = unittest.TestSuite() # 存储用例 # suite.addTest(loader.loadTestsFromTestCase(TestMathMethod)) # 传入测试类名,会自动找到这个类下所有的测试用例 # # runner = unittest.TextTestRunner() # runner.run(suite) # -------------------------------------------------- # 方法3,模块中定义了多个测试类,导入和传参的时候具体到模块名,不用写类名 # loader = unittest.TestLoader() # suite = unittest.TestSuite() # suite.addTest(loader.loadTestsFromModule(class_unittest)) # # runner = unittest.TextTestRunner() # runner.run(suite) # ------------------------------------------------------------ # 把测试结果输出到文件中 # 加载用例 # loader = unittest.TestLoader() # suite = unittest.TestSuite() # suite.addTest(loader.loadTestsFromModule(class_unittest)) # 执行用例,把测试结果输出到文件中 # with open("test.text", "w+", encoding="utf-8") as file: # runner = unittest.TextTestRunner(verbosity=2) # runner.run(suite) # print(file.closed) # 判断文件是否关闭状态 # -------------------------------------------------------- suite = unittest.TestSuite() suite.addTest(TestMathMethod("test_add_two_positive")) suite.addTest(TestMathMethod("test_add_negative")) suite.addTest(TestMathMethod("test_add_two_zero")) test_case = [TestMulti("test_multi_positive"), TestMulti("test_multi_negative"), TestMulti("test_multi_zero")] suite.addTests(test_case) with open("result.html", "wb") as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file, title="单元测试报告", description="未来测试大佬") runner.run(suite)