简体   繁体   English

Jmeter中REST服务测试的可变路径参数

[英]Variable Path Param for REST service testing in Jmeter

I'm testing RESt service which has path parameter.我正在测试具有路径参数的 REST 服务。

/my-service/v1/Customer/order/{ordernumber}

I want to increment the number by 1 for each request.我想为每个请求将数字增加 1。 How to achieve this in Jmeter?如何在 Jmeter 中实现这一点? Till now i had been passing a fixed path param, therefor our test result were on only one input parameter.直到现在我一直在传递一个固定的路径参数,因此我们的测试结果只有一个输入参数。

/my-service/v1/Customer/order/5247710017785924

The good point to start with is putting your initial order value into User Defined Variable开始时最好将您的初始订单值放入用户定义的变量中

Given start order as "5247710017785924" you need to create an "ordernumber" variable and set it's value to 5247710017785924.给定起始订单为“5247710017785924”,您需要创建一个“ordernumber”变量并将其值设置为5247710017785924。

After each request you can increment variable value by adding BeanShell postprocessor to your HTTP Sampler with following code:在每个请求之后,您可以通过使用以下代码将 BeanShell 后处理器添加到 HTTP 采样器来增加变量值:

long ordernumber = Long.parseLong(vars.get("ordernumber"));
ordernumber++;
vars.put("ordernumber",String.valueOf(ordernumber));

And set ordernumber in your HTTP Sampler path as并将您的 HTTP Sampler 路径中的 ordernumber 设置为

/my-service/v1/Customer/order/${ordernumber}

None of the solutions worked for me.没有一个解决方案对我有用。 Here is what I did这是我所做的

  1. Define HTTP request as shown below and add path /api/v2/state/find/${id} to the request定义如下所示的 HTTP 请求并将路径/api/v2/state/find/${id}到请求中
  2. Right click on HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value右键单击HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value
  3. Start HTTP request, this should work启动 HTTP 请求,这应该可以工作

HTTP 请求

用户参数

使用 JMeter Counter组件来增加变量。

This question is path parameter related, where the value of the order number is incremented by 1 in each successive request.这个问题与路径参数有关,其中订单号的值在每个连续请求中递增 1。 But I faced a scenario where I got a list of order numbers and I had to make request for those order numbers.但是我遇到了一个场景,我得到了一个订单号列表,我不得不请求这些订单号。 So, I am gonna answer this question with respect to that, this solution can be applied in both the scenarios.所以,我要回答这个问题,这个解决方案可以应用于这两种情况。

What I did is put all the parameter paths in a CSV file, like this -我所做的是将所有参数路径放在一个 CSV 文件中,就像这样 -

/my-service/v1/Customer/order/5247710017785924
/my-service/v1/Customer/order/5247710017785976
/my-service/v1/Customer/order/5247710017785984
/my-service/v1/Customer/order/5247710017785991

Then I iterated through the list of paths in the CSHTTPle and made http request to the server.然后我遍历 CSHTTPle 中的路径列表并向服务器发出 http 请求。 To know how to iterate through the CSV file and make http request in Jmeter, you can check this link:要了解如何遍历 CSV 文件并在 Jmeter 中发出 http 请求,您可以查看此链接:

https://stackoverflow.com/a/47159022/5892553 https://stackoverflow.com/a/47159022/5892553

I used a BeanShell PreProcessor to generate an id我使用了一个BeanShell PreProcessor来生成一个 id

vars.put("id", UUID.randomUUID().toString());

Then used the path Http Request然后使用路径Http Request

/api/v1/event/${id}/

BINGO!!!宾果游戏!!!

You can use a JMeter Counter:您可以使用 JMeter 计数器:

  1. right click on your Thread Group (under the Test Plan)右键单击您的线程组(在测试计划下)
  2. select Add–>Config Element–>Counter选择添加->配置元素->计数器
  3. set the Starting value (0), Increment (1), Maximum value, Exported Variable Name ("ordernumber")设置起始值 (0)、增量 (1)、最大值、导出变量名称 ("ordernumber")

Then you can use the exported variable name as path param: /my-service/v1/Customer/order/${ordernumber}然后您可以使用导出的变量名称作为路径参数:/my-service/v1/Customer/order/${ordernumber}

Referring to the contributor who suggests using the user parameters to put customer or order id's into the API. 参考建议使用用户参数将客户或订单ID放入API的贡献者。 This works but unless there is a way of bulk loading the users this is not scalable for larger amounts of data. 这是有效的,但除非有一种批量加载用户的方法,否则这对于大量数据是不可扩展的。 Useful if you only want a few (up to ten maybe) as they all have to be entered manually. 如果您只想要一些(最多可能十个),因为它们都必须手动输入,这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM