locust_关联

from locust import HttpLocust, TaskSet, task
import os

'''
实现场景:先登录(只登录一次),然后访问->我的地盘页->产品页->项目页
访问我的地盘页面权重为2,产品页和项目页权重各为1
'''

class UserBehavior(TaskSet):
    '''蝗虫行为类'''
    def _login(self):
        '''登录方法'''
        # host = 'http://192.168.x.xx:80'  # 禅道的服务器地
        loginUrl ="/zentao/user-login.html/"
        h = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
            "Content-Type": "application/x-www-form-urlencoded",
            }
        body = {"account": "yoyo",  # 你自己的账号
                "password": "******",    # 你自己的密码
                "keepLogin[]": "on",
                "referer": "/zentao/my/"
                }
        r = self.client.post(loginUrl, data=body, headers=h)
        print(r.text)
        assert "parent.location='/zentao/index.html'" in r.text

    def on_start(self):
        '''任务开始准备工作:只登录一次'''
        self._login()

    # 任务1-我的地盘
    @task(2)
    def zentao_my(self):
        print("---访问页面-我的地盘---")
        r = self.client.get("/zentao/my/")
        assert "我的地盘" in r.text

    # 任务2-产品页
    @task(1)
    def zentao_product(self):
        print("---访问页面-产品页---")
        r = self.client.get("/zentao/product-browse.html/")
        assert "需求列表" in r.text

    # 任务3-项目
    @task(1)
    def zentao_prject(self):
        print("---访问页面-项目---")
        r = self.client.get("/zentao/project/")
        assert "项目首页" in r.text


class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000

if __name__ == "__main__":

    os.system("locust -f locustfile.py --host=http://192.168.x.xx:80")
原文地址:https://www.cnblogs.com/xiaochuichui/p/13280949.html