From locust import httplocust, TaskSet, task
Import JSON
From common import Readconfig
Class Userbehavior (TaskSet): # define user behavior
def on_start (self): # The On_Start method is called when the impersonated user starts executing the Taskset class
Pass
def get_headers (self):
"" Member Login "" "
headers = {
"Content-type": "Application/json",
"Channel": "Shop"
}
BODY = {
"Unionid": Readconfig.unionid # Such use will be error, no common ...
}
res = self.client.post ('/customers/login ', headers=headers, Data=json.dumps (body)). Text
Assert ' success ' in Res # assertion to determine if interface return succeeded
res = json.loads (res)
UID = res[' data ' [' UID ']
Ukey = res[' data ' [' Ukey ']
return [UID, Ukey] # member Login return UID and Ukey
@task (1) # @task () decorate the method as a task; 1 indicates that a locust instance is selected to perform the weights, the higher the value, the higher the execution frequency;
def members_can_withdraw_account_information (self): # a behavior
"" "to enable members to obtain membership status" "
res = Self.get_headers ()
headers = {
"Channel": "Shop",
"UID": Str (res[0]),
"Ukey": res[1]
}
Response = Self.client.put ("/customers/customer_state",
headers=headers). Text # Client.get () is used to refer to the requested path, which can be added headers,params,body parameters;
Assert ' success ' in response
@task (1)
def get_member_account_withdrawal_details (self):
"" "to obtain the member account withdrawal details" "
res = Self.get_headers ()
headers = {
"Channel": "Shop",
"UID": Str (res[0]),
"Ukey": res[1]
}
params = {
"Page": "10",
"Size": "10"
}
Response = Self.client.get ("/customers/account/withdraw/log", Headers=headers, params=params). Text
Assert ' success ' in response
Class Websiteuser (Httplocust): # Set performance test;
Host = "Http://dev.sign.lixiaofeng.com"
Task_set = Userbehavior # points to a defined user behavior class;
min_wait = 3000 # The lower bound of the wait time between tasks performed by the user: milliseconds;
max_wait = 6000 # The upper bound of the wait time between tasks performed by the user: milliseconds;
Python Locust Performance test: Locust Association---Extract the returned data and use