当前位置:网站首页>JMeter's own functions are not enough? Why don't you develop one yourself

JMeter's own functions are not enough? Why don't you develop one yourself

2022-07-07 06:12:00 Code classmate software test

stay Jmeter In the function assistant , There are many built-in functions , such as Random、UUID、time wait . Using these functions can help us generate some data quickly , Do some logical processing . It's very convenient to use .

But in the actual interface testing process , There are many needs ,Jmeter Built in functions may not meet our needs . At this time, we can write our own code , Expand Jmeter Built in functions for .

For example, now I want to Jmeter Add a simple summation function to , The code logic is very simple , The general code is as follows :

public int sum(int a, int b){
return a + b;
}
Next, let's see how to encapsulate the custom logic into Jmeter Built in functions for .

New project
stay idea New China Java engineering , And then put Jmeter At the heart of jar package ApacheJMeter_core.jar( stay jmeter Of lib/ext You can find ) Import into project

 picture

stay src New under the directory package, It should be noted that package Your name must contain ".functions", such as "com.mtx.jmeter.functions",JMeter Design so that some core classes can be in non UI Can be loaded when running in the mode of , These classes will be loaded first . These classes are loaded through naming rules . All implementation function The class of must contain ".functions", Otherwise, the customized function will not take effect .

Create a custom function class
Create a new class , Inherit Jmeter The abstract class of AbstractFunction. In function class , The following properties and methods need to be defined .

attribute

List of parameter names of custom functions ,List type

The name of the custom function ,String type

Save an array of user input data ,CompoundVariable[] type

Method

execute: Execute the logic of the function

setParameters: Get the parameters entered by the user

getReferenceKey: Return the name of the custom function

getArgumentDesc: Return the description of all parameter names of the custom function

The specific code is as follows :

 picture

Export code as jar package
stay idea Set up the archive of the project artifacts, And then through build, Export code as jar package , Then put Jmeter Of lib/ext Under the table of contents , Restart Jmeter.

You can see the function name and parameters just now in the function assistant .

 picture

Input parameters , Operation function , You can see the calculation results .

原网站

版权声明
本文为[Code classmate software test]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070111425983.html