discover(start_dir,pattern = "test*.py”,top_level_dir = None)
start_dir:待测试的模块名或测试用例目录
pattern= "test*.py”:测试用例文件名的匹配原则
top_level_dir = None:测试模块的顶层目录,默认None
例如unittest1目录下的包含test的python文件:
import unittest # 定义测试用例的目录为当前目录中的unittest1目录 test_dir = './unittest1' suits = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') if __name__ == '__main__': runner = unittest.TextTestRunner() runner.run(suits)