封装测试套件运行某个目录下所有用例并出html报告 importmport os,time case path = os.path.join(os.path.dirname(_ file_ ), 'demo_unittest') report path = os.path.join(os.path,dirnane(_ file__ ), 'report') defcreatesuite( ): suite = unittest.TestSuite( ) discover = unittestdefaultTestLoader.discover(casepath,pattern='test*.py',top_level_dir=None) print (discover ) for test suite in discover: for test case in test_suite : suite.addTests (test_case ) return suite now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime()) filename = report_path+"/”+now+"_result.html" with open(filename, 'wb') as fp: runner = HTMLTestRunner( stream=fp, title='接口测试报告', description='搜索用例执行情况:') runner.run(create_suite()) fp.close()