學(xué)習(xí)筆記-locust高級(jí)用法

1、關(guān)聯(lián)session

在某些請求中,需要攜帶之前response中提取的參數(shù),常見場景就是session_id。Python中可用通過re正則匹配,對(duì)于返回的html頁面,可用采用lxml庫來定位獲取需要的參數(shù);

from locust import HttpLocust, TaskSet, task
from lxml import etree

class WebsiteTasks(TaskSet):

    def get_session(self,html): #關(guān)聯(lián)例子
        tages = etree.HTML(html)
        return tages.xpath("http://div[@class='btnbox']/input[@name='session']/@value")[0]

2、參數(shù)化

保證并發(fā)測試數(shù)據(jù)唯一性,不循環(huán)取數(shù)據(jù);采用隊(duì)列

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

保證并發(fā)測試數(shù)據(jù)唯一性,循環(huán)取數(shù)據(jù);
所有并發(fā)虛擬用戶共享同一份測試數(shù)據(jù),保證并發(fā)虛擬用戶使用的數(shù)據(jù)不重復(fù),并且數(shù)據(jù)可循環(huán)重復(fù)使用;

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斷言對(duì)接口返回值中的max字段進(jìn)行斷言
            if response.status_code ==200:                   #對(duì)http響應(yīng)碼是否200進(jìn)行判斷
                response.success()
            else:
                response.failure("GetActConfig[Failed!]")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄 ·大型網(wǎng)站軟件系統(tǒng)的特點(diǎn) ·大型網(wǎng)站架構(gòu)演化發(fā)展歷程 ·初始階段的網(wǎng)站架構(gòu) ·需求/解決問題 ·架構(gòu) ·應(yīng)用...
    zhyang0918閱讀 2,853評(píng)論 0 16
  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,380評(píng)論 0 5
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,033評(píng)論 0 11
  • Python語言特性 1 Python的函數(shù)參數(shù)傳遞 看兩個(gè)如下例子,分析運(yùn)行結(jié)果: 代碼一: a = 1 def...
    伊森H閱讀 3,184評(píng)論 0 15
  • 哭喊著 你睜眼 歡笑著 走向前 困惑著 你擱淺 沉默著 都幻滅 在沉浮之間尋找愛的光線 陰霾邊沿觸碰藍(lán)天 像煙火炙...
    八咫鳥閱讀 201評(píng)論 0 0

友情鏈接更多精彩內(nèi)容