Java Jmeter中REST服务测试的可变路径参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19694443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable Path Param for REST service testing in Jmeter
提问by Pankaj
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. How to achieve this in Jmeter? Till now i had been passing a fixed path param, therefor our test result were on only one input parameter.
我想为每个请求将数字增加 1。如何在 Jmeter 中实现这一点?直到现在我一直在传递一个固定的路径参数,因此我们的测试结果只有一个输入参数。
/my-service/v1/Customer/order/5247710017785924
采纳答案by ragnor
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}
回答by Rito
This question is path parameter related, where the value of the order number is incremented by 1 in each successive request. 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.
这个问题与路径参数有关,其中订单号的值在每个连续请求中递增 1。但是我遇到了一个场景,我得到了一个订单号列表,我不得不请求这些订单号。所以,我要回答这个问题,这个解决方案可以应用于这两种情况。
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. To know how to iterate through the CSV file and make http request in Jmeter, you can check this link:
然后我遍历 CSHTTPle 中的路径列表并向服务器发出 http 请求。要了解如何遍历 CSV 文件并在 Jmeter 中发出 http 请求,您可以查看此链接:
回答by Jadda
None of the solutions worked for me. Here is what I did
没有一个解决方案对我有用。这是我所做的
- Define HTTP request as shown below and add path
/api/v2/state/find/${id}
to the request - Right click on
HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value
- Start HTTP request, this should work
- 如下图定义HTTP请求并添加
/api/v2/state/find/${id}
请求路径 - 右键单击
HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value
- 启动 HTTP 请求,这应该可以工作