Interview Questions

How do I pass parameters into my Test scripts?

Apache JMeter FAQ


(Continued from previous question...)

How do I pass parameters into my Test scripts?

Q: How do I pass parameters into my Test scripts? I want to be able to use the same script to test with different numbers of threads and loops, and I don't want to have to change the script each time.



Answer:
as explained above, you can use functions and variables just about anywhere in the test plan. So if you want to pass in a value a run-time, just use the property() function, which reads the value of a JMeter property.

In order to define the property so JMeter can read it, define it on the command line as follows:

jmeter -Jproperty_name=property_value

For example:

jmeter -Jhost2=www.zzzyy.com -Jhost1=www.jmeter-rules.net

These values can then be read in the test plan using:

{{{ ${
property(host1)} and

*

${property(host2)} }}}


Note: Thread Groups are slightly different from other test elements, because their settings have to be determined before the test starts. This means that you cannot use variables defined in a User Parameters form. But the property() function works in Thread Groups, and you can use variables defined on the Test Plan.

For example, you could define the TestPlan variable:

THREADS ${P(threads,10)}

and then use ${THREADS} in the ThreadGroup test element.

Elsewhere, you can use function calls, or variable references to User Parameters (which in turn could be functions), or variable references to variables set up by functions earlier in the test. There's more than one way to do it.

Suppose you want to be able to vary the number of threads in a test plan. Choose a suitable property name, say group1.threads. Replace the thread count in the GUI (or the JMX, if you're feeling brave!) with the following function call:

${__property(group1.threads)}

Then, when starting JMeter, define the property on the command line:

jmeter -Jgroup1.threads=12345

It can be useful to put default settings into the jmeter property file, so you only need to supply differences on the command line.

{{{ # defaults in jmeter.properties

* group1.threads=10 group1.loops=100 group1.rampup=10 }}}

Then just do jmeter -Jgroup1.loops=1000 for example.

Versions of JMeter after 1.9.1 have a new version of the property() function which allows a default value to be supplied, in case the property is not found:

${__property(group2.threads,,defaultvalue)}

There is also a shorthand version called P(), which you can use as follows:

${__P(group2.threads,100)}

if you omit the value, it defaults to 1

(Continued on next question...)

Other Interview Questions