CSDN话题挑战赛第2期
参赛话题:学习笔记
TestRunner的工作流程:
遍历测试类中的测试用例,调用用例对象的run方法
将测试用例的结果保存到TestResult中
特别注意:
如果测试粒度按照文件为单位,那么文件中的测试类执行顺序就能得到保证
如果测试粒度按照测试类为单位,那么测试类中的测试用例执行顺序就能得到保证
如果测试粒度按照测试用例为单位,并且用例与用例之间有关联,那么测试用例执行顺序得不到保证
import unittest
from unittest import TestCase,TestSuite,TestLoader,TestResult
'''
粒度——》测试文件
粒度——》测试类
粒度——》用例级别
'''
from concurrent.futures.thread import ThreadPoolExecutor
#todo 加载用例到测试套件中
suite=unittest.defaultTestLoader.discover(r'D:\project_development\测试开发\day11\testcases')
print('-------------------------单线程执行--------------------------------------')
#todo 创建一个测试结果记录器
result=unittest.TestResult()
#todo 执行测试套件
suite.run(result)
print(suite)
print('用例执行的结果,',result)
import time
import unittest
from ddt import ddt,data
@ddt
class DemoA1(unittest.TestCase):
@data(1,2,3,6)
def test_demoa1(self,item):
time.sleep(1)
print('---------test_demoa1--------',item)
@ddt
class DemoA2(unittest.TestCase):
@data(1,2,3,6)
def test_demoa2(self,item):
time.sleep(1)
print('---------test_demoa2--------',item)
import time
import unittest
from ddt import ddt,data
@ddt
class DemoB1(unittest.TestCase):
@data(1,2,3,6)
def test_demob1(self,item):
time.sleep(1)
print('---------test_demob1--------',item)
@ddt
class DemoB2(unittest.TestCase):
@data(1,2,3,6)
def test_demob2(self,item):
time.sleep(1)
print('---------test_demob1--------',item)
套件可以直接执行,套件中有run方法
model_suite=[m for m in suite]
result=unittest.TestResult()
创建一个线程池对象,一共创建了4个模块,所以max_workers最大为4
with ThreadPoolExecutor(max_workers=4) as tp: #创建一个线程池对象
for i in model_suite:
#i.run(result)
tp.submit(i.run,result)
class_suite=[]
for m in suite:
for c in m:
class_suite.append(c)
result=unittest.TestResult()
with ThreadPoolExecutor(max_workers=8) as tp:
for i in class_suite:
tp.submit(i.run,result)
case_list=[]
for m in suite:
for c in m:
for i in c:
case_list.append(i)
result=unittest.TestResult()
with ThreadPoolExecutor(max_workers=32) as tp:
for i in case_list:
tp.submit(i.run,result)
import unittest
from unittest import TestCase,TestSuite,TestLoader,TestResult
'''
粒度——》用例级别
粒度——》测试类
'''
from concurrent.futures.thread import ThreadPoolExecutor
#todo 加载用例到测试套件中
suite=unittest.defaultTestLoader.discover(r'D:\project_development\测试开发\day11\testcases')
print('-------------------------多线程执行--------------------------------------')
def run_cases(suite,model):
#判断任务执行粒度
task_list = []
if model=='case':
for m in suite:
for c in m:
for i in c:
task_list.append(i)
elif model=='cls':
for m in suite:
for c in m:
task_list.append(c)
else:
task_list=[i for i in suite]
result=unittest.TestResult()
with ThreadPoolExecutor(max_workers=8) as tp:
for i in task_list:
tp.submit(i.run,result)
run_cases(suite,'asd')