Locust學習筆記(2) - 登錄腳本的實現,多用戶隨機登錄

1.實現登錄的基本功能,輸出響應,證明腳本正確
2.多用戶隨機登錄:構造隨機數據,doLogin方法中用隨機數據進行請求
3.添加初始化方法on_start:類似於構造方法,每個虛擬用戶只運行一次。
4.添加檢查點(斷言)
    -  在請求方法中設置catch_response參數爲True
    -  調用sucess或者failure方法標註成功或失敗
from locust import HttpLocust, TaskSet, task, between
from random import randint

class TestLogin(TaskSet):
   # 每個虛擬用戶執行一次
def on_start(self): self.login_data = [{"username": "user1", "password": "pwd1"}, {"username": "user2", "password": "pwd2"}, {"username": "user3", "password": "pwd3"}] self.ranIndex = randint(0, len(self.login_data) - 1) print("----------------------") @task def doLogin(self): print(self.login_data[self.ranIndex]) response = self.client.post("/admin/", data=self.login_data[self.ranIndex],catch_response=Ture) print(response.text) # 斷言 if "login-pass" in response.text: response.success() else: response.failure("Can not login!") class WebSite(HttpLocust): task_set = TestLogin wait_time = between(3, 7)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章