Bastian
Bastian

Reputation: 1237

Call function from different sampler in JMeter - Groovy

I have a sampler that get data from DB (budget), and I have some assertions that I made using JSR223 assertion. In each assertion I write the same function (check_budget) and to each assertion I pass different values (start_budget, end_budget etc)

The problem is that I duplicate the code for each assertion and it is not friendly for maintenance.(if logic change need to change at 7 duplicate functions) It there a way to create a sampler and write generic function in it (calc_budget) and to call it from each assertion, like creating a class in java and perform import?

enter image description here enter image description here

Upvotes: 0

Views: 1270

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

In Groovy you have evaluate() function, so given you stored it into a JMeter Variable or JMeter Property or into a file you can call it like:

evaluate(vars.get('your_var'))

or

evaluate(new File('your_test.groovy'))

See Scripting JMeter Assertions in Groovy - A Tutorial article for more information.

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58872

In that case, and in general also, you can keep groovy script in a script file and call the same script file from all JSR223 elements

Script File Name of a file to be used as a JSR223 script, if a relative file path is used, then it will be relative to directory referenced by "user.dir" System property

Upvotes: 1

Related Questions