unittest执行用例方法:
1、单个py文件
if __name__ == "__main__": unittest.main()
2、多个py文件,引用测试用例,使用测试套件,执行所加载到套件中用例
import unittest from test_case.test_study1 import Test_Study1 from test_case.test_study2_skip import Test_Study2 # 实例化测试套件及加载用例 suit = unittest.TestSuite() suit.addTest(Test_Study1(‘test_case1‘)) suit.addTest(Test_Study1(‘test_case2‘)) suit.addTest(Test_Study2(‘test_case4‘)) # 实例化runner及执行套件中用例 runner = unittest.TextTestRunner() runner.run(suit)
3、使用discover,批量执行
case_dir:要执行的用例目录
pattern:匹配测试用例规则,test*.py即,匹配所有test开头的脚本
import unittest import os case_path = os.path.join(os.path.dirname(os.getcwd()), "test_case") discover = unittest.defaultTestLoader.discover(case_path, pattern="test*.py", ) runner = unittest.TextTestRunner() runner.run(discover)
4、加载测试类
import unittest from test_case.test_study1 import Test_Study1 from test_case.test_study2_skip import Test_Study2 suite1 = unittest.TestLoader().loadTestsFromTestCase(Test_Study1) suite2 = unittest.TestLoader().loadTestsFromTestCase(Test_Study2) suite = unittest.TestSuite([suite1, suite2]) runner = unittest.TextTestRunner() runner.run(suite)
原文地址:https://www.cnblogs.com/juankai/p/11580111.html
时间: 2024-10-09 19:11:02