TaskSet类和TaskSequence类可用于嵌套<可以在TaskSequences中嵌套TaskSets,反之亦然>;
from locust import TaskSet, task, HttpLocust, TaskSequence, seq_task
import subprocess
class WebUser(TaskSet):
@task(5)
def first_task(self):
print('执行5次;')
@task(2)
class IosUser(TaskSet):
@task(1)
def second_task(self):
print('1次')
@task(2)
def three_task(self):
print('2次')
self.interrupt()
@task(2)
class AndroidUser(TaskSequence):
@seq_task(2)
@task(1)
def android_task(self):
print('这是android用户;')
self.interrupt()
@seq_task(1)
@task(1)
def ios_task(self):
print('这是ios用户;')
class LocustFun(HttpLocust):
host = 'https://passport.cnblogs.com'
task_set = WebUser
max_wait = 6000
min_wait = 3000
if __name__ == '__main__':
subprocess.check_call(
'locust -f G:\Interface_framework_pytest\\tmp\\test2.py --no-web -c 10 -r 1')
self.interrupt()函数用于将执行移交给父TaskSet,需要在嵌套中使用,在Locust类的task_set属性指向的主TaskSet中使用会报错:InterruptTaskSet exception具有中断功能,可以与任务加权一起定义模拟用户离开论坛的可能性;
TaskSequence类,装饰器@seq_task(1),控制任务的执行顺序,参数:number;
转载于:https://www.cnblogs.com/changqing8023/p/10204951.html