unittest自定义运行全量case or 运行指定的单个或多个case
1 import unittest
2 import os
3 from case.zufang.test_api_area_rentProlist import Zf1
4
5
6 case_path = os.path.join(os.path.abspath("."), "case")
7
8 #执行单条用例
9 def addSuite(caseList):
10 suite = unittest.TestSuite()
11 suite.addTests(caseList)
12 return suite
13
14 #执行全部测试用例
15 def call_case():
16 discover = unittest.defaultTestLoader.discover(case_path,pattern="test*.py",top_level_dir=None)
17 return discover
18
19 #自定义case的运行模式
20 def run_modle(runmode,caseList=[]):
21 runner = unittest.TextTestRunner()
22 #执行全部case
23 if runmode == 1:
24 runner.run(call_case())
25 #执行定义的case
26 elif runmode == 2:
27 runner.run(addSuite(caseList))
28
29 if __name__ == "__main__":
30
31 caseList=[Zf1("test02_area_rent")]
32 run_modle(2,caseList)
33 # run_modle(1)
responseCheck支持自定义运行模式(全量运行case or 自定义运行case)
run_modle(1)运行全量case
run_modle(2)自定运行的caseList
示例:
1.运行全量case
1 if __name__ == "__main__":
2
3 run_modle(1)
2.自定义运行case
a.在runTest.py导入你case所在的文件如:
from case.zufang.test_api_area_rentProlist import Zf1
b.在main方法中添加要运行的case
if __name__ == "__main__":
caseList=[Zf1("test02_area_rent")]
run_modle(2,caseList)
说明:Zf1表示测试类名,test02_area_rent是类名中的方法名