美文网首页
学习笔记-locust高级用法

学习笔记-locust高级用法

作者: sofiiii | 来源:发表于2020-03-20 15:41 被阅读0次

    1、关联session

    在某些请求中,需要携带之前response中提取的参数,常见场景就是session_id。Python中可用通过re正则匹配,对于返回的html页面,可用采用lxml库来定位获取需要的参数;

    from locust import HttpLocust, TaskSet, task
    from lxml import etree
    
    class WebsiteTasks(TaskSet):
    
        def get_session(self,html): #关联例子
            tages = etree.HTML(html)
            return tages.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0]
    

    2、参数化

    保证并发测试数据唯一性,不循环取数据;采用队列

    from locust import TaskSet, task, HttpLocust
    import queue
    class UserBehavior(TaskSet):
        @task
        def test_register(self):
            try:
                data = self.locust.user_data_queue.get()
            except queue.Empty:
                print('account data run out, test ended.')
                exit(0)
            print('register with user: {}, pwd: {}'\
                .format(data['username'], data['password']))
            payload = {
                'username': data['username'],
                'password': data['password']
            }
            self.client.post('/register', data=payload)
    class WebsiteUser(HttpLocust):
        host = 'http://debugtalk.com'
        task_set = UserBehavior
        user_data_queue = queue.Queue()
        for index in range(100):
            data = {
                "username": "test%04d" % index,
                "password": "pwd%04d" % index,
                "email": "test%04d@debugtalk.test" % index,
                "phone": "186%08d" % index,
            }
            user_data_queue.put_nowait(data)
        min_wait = 1000
        max_wait = 3000
    

    保证并发测试数据唯一性,循环取数据;
    所有并发虚拟用户共享同一份测试数据,保证并发虚拟用户使用的数据不重复,并且数据可循环重复使用;

    from locust import TaskSet, task, HttpLocust
    import queue
    class UserBehavior(TaskSet):
        @task
        def test_register(self):
            try:
                data = self.locust.user_data_queue.get()
            except queue.Empty:
                print('account data run out, test ended')
                exit(0)
            print('register with user: {0}, pwd: {1}' .format(data['username'], data['password']))
            payload = {
                'username': data['username'],
                'password': data['password']
            }
            self.client.post('/register', data=payload)
            self.locust.user_data_queue.put_nowait(data)
    class WebsiteUser(HttpLocust):
        host = 'http://debugtalk.com'
        task_set = UserBehavior
        user_data_queue = queue.Queue()
        for index in range(100):
            data = {
                "username": "test%04d" % index,
                "password": "pwd%04d" % index,
                "email": "test%04d@debugtalk.test" % index,
                "phone": "186%08d" % index,
            }
            user_data_queue.put_nowait(data)
        min_wait = 1000
        max_wait = 3000
    

    3、断言

    @task
    def all_interface(self):
         #豆瓣图书api为例子
         with  self.client.get("https://api.douban.com/v2/book/1220562",name="/LhcActivity/GetActConfig",catch_response=True) as response:
          assert response.json()['rating']['max']==10            #python断言对接口返回值中的max字段进行断言
                if response.status_code ==200:                   #对http响应码是否200进行判断
                    response.success()
                else:
                    response.failure("GetActConfig[Failed!]")
    

    相关文章

      网友评论

          本文标题:学习笔记-locust高级用法

          本文链接:https://www.haomeiwen.com/subject/vdyjyhtx.html