栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

locust 断言和参数化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

locust 断言和参数化

断言404失败的测试用例请求

catch_response = True :布尔类型,如果设置为 True, 允许该请求被标记为失败。

from locust import HttpUser, task, TaskSet 

# 定义用户行为(测试用例)
class TestCase(TaskSet):
    @task()
    def index(self):
        self.client.get('/HelloWorld') # 访问路径

    @task()
    def index1(self):
        with self.client.get('/HelloWorld4', catch_response=True) as response:
            if response.status_code == 404:
                response.failure('index is error')

# 设置场景
class WebsiteUser(HttpUser):
    tasks = [TestCase] # 将测试用例添加到测试套件中
    min_wait = 1
    max_wait = 2
    host = 'http://127.0.0.1:5000'

参数化-百度搜索例子
from locust import HttpUser, task, TaskSet
from random import randint

# 定义用户行为(测试用例)
class TestCase(TaskSet):
    @task()
    def index(self):
        key =['locust','接口测试']

        sj = randint(0,1) # 随机返回0,1

        url_data = '/s?wd='+ key[sj] # 拼接路径
        
        #catch_response = True :布尔类型,如果设置为 True, 允许该请求被标记为失败。
        with self.client.get(url_data, catch_response=True) as response:
            if response.status_code == 200: # 如果状态码是200,返回页面数据
                print(response.content) # 返回页面数据
                response.success()

            else: # 失败时记录
                response.failure('index is error')

# 设置场景
class WebsiteUser(HttpUser):
    tasks = [TestCase] # 将测试用例添加到测试套件中
    min_wait = 1
    max_wait = 2
    host = 'https://www.baidu.com' # 百度url

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992936.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号