Locust 做多接口测试

 

 

 

 

 

import requests
from locust import HttpLocust, TaskSet, task, HttpUser


def mock_phone():
    """
    :return:
    """
    res = requests.post(url='http://121.40.32.213:5000/api/v1/mock_data')
    phone = res.json()['data']['mock测试数据']['手机号']
    # pprint.pprint(res.json())
    print('用于测试的手机号', phone)
    return phone


class Run(TaskSet):
    """
    # task装饰该方法为一个事务方法的参数用于指定该行为的执行权重。参数越大,每次被虚拟用户执行概率越高,不设置默认是1
    """
    @task(1)  # 任务项
    def test_login(self):
        payload = {
            'mobile': mock_phone(),
            'captcha': '12345678'
        }
        url = '/addons/miniform/login/mobilelogin'
        res = self.client.post(url, data=payload)
        if res.json()['code'] == 1:
            print('登陆成功')
            print(res.json())
            header = {
                'token': res.json()['data']['token']
            }

            return header
        else:
            print('登陆失败!')

    @task(1)
    # 2查询用户我的-奖章-奖章数量
    def test_jian_zhang(self):
        payload = {}
        url = '/api/v1/my/Medal/mycount'
        print(1111111, self.test_login())       # 用于检测token是否传递过来
        res = self.client.post(url, data=payload, headers=self.test_login())
        if res.json()['code'] == 1:
            print('获取数据成功')
            print(res.json())
        else:
            print('获取数据失败!')


# # 这个类类似设置性能测试,继承HttpLocust
# class websitUser(HttpLocust):
#     # 指向一个上面定义的用户行为类
#     task_set = [Run]
#     host = 'https://sxty.zjzxsl.com/'
#     # 执行事物之间用户等待时间的下界,单位毫秒,相当于lr中的think time
#     min_wait = 1000
#     max_wait = 6000
#
class WebSiteUser(HttpUser):
    tasks = [Run]
    max_wait = 5000
    min_wait = 1000


if __name__ == '__main__':

    import os
    os.system('locust -f demo_06.py --host=********')

 

posted @ 2022-08-10 17:53  陈晓猛  阅读(303)  评论(0编辑  收藏  举报