python locust--Setups, Teardowns, on_start, and on_stop .(用例执行前执行一次请求啥的)

创建一个locust测试脚本,如下:

from locust import HttpLocust, TaskSet, task


class UserBehavior(TaskSet):
def setup(self):
print('task setup')

def teardown(self):
print('task teardown')

def on_start(self):
# 虚拟用户启动Task时运行
print('start')

def on_stop(self):
# 虚拟用户结束Task时运行
print('end')

@task(2)
def index(self):
self.client.get("/")

@task(1)
def profile(self):
self.client.get("/profile")


class WebsiteUser(HttpLocust):
def setup(self):
print('locust setup')

def teardown(self):
print('locust teardown')
task_set = UserBehavior
min_wait = 5000
max_wait = 9000


if __name__ == '__main__':
pass
说明:

Locust类有setup和teardown,TaskSet类有setup、teardown、on_start、on_stop。

每次启动locust时运行setup方法,退出时运行teardown方法,locust执行TaskSet时运行TaskSet的setup方法,退出时运行teardown方法,每个虚拟用户执行操作时运行on_start方法,退出时执行on_stop方法,运行上面的脚本,执行顺序如下:

执行顺序如下:

Locust setup

TaskSet setup

TaskSet on_start

TaskSet tasks

TaskSet on_stop

TaskSet teardown

Locust teardown
————————————————
版权声明:本文为CSDN博主「梦入玄机」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36255988/article/details/82622044

原文地址:https://www.cnblogs.com/kaibindirver/p/11774651.html