Jmeter: Using jmeter Junit Request with variables

2019-05-24 10:34发布

问题:

As I said in the question, is it possible to pass jmeter variables (passed through -J) into a JUnit Request in order that it can be used internally to the junit test?

It sounds feasible as the Apache Junit Request docs indicate that:

... JMeter currently runs the test methods directly, rather than leaving it to JUnit. ...

This would indicate that jmeter could control passing parameters but I've found no supporting documentation and there's no obvious mechanism through the JUnit Request sampler config.

Background

I was hoping to use CSV Data Set Config to load a user pool (as in Victor Klepikovskiy excellent top tips) and have a randomly obtained user provided to my unit-tests (for use across the thread/ loop). I've found an alternative way of doing what I want (but use my own user pool accessed from within the junit tests). I know you could manage the login through Http Request too (as above) but we amanage the rest-service calls internally to junit so that they can be run as junit tests as well. I was interested in being as non-intrusive as possible.

A second reason for doing so might be to have a single junit test method but have it parameterised to suit the scenario.

Addendum

For what it's worth, I'm happy with my current alternative approach but still interested in how you might be able to do this for future reference. I'm fairly new to jmeter but expect to see a fair bit more of it. Was also thinking of looking at the pre-processors but not sure how these could interface with the Junit Request.

回答1:

I have tried to do exactly what you are doing (although, never specifically used -J). The short answer is I have still not found a way to make it work to pass variables into a JUnit Request. I believe it is not currently supported.

At the time, the only way I saw of doing this was passing a parameter (e.g., ${__threadNum} or even from CSV Data Set Config) into the Constructor String Label and writing my constructor appropriately. However, I only got the string literal "${__threadNum}". I contacted support and they mentioned to me that this will not work. I forget exactly what they said and cannot find the email. But the main idea was that a JUnit Request is instantiated or created before the parameter in the Conostructor String Label is parsed. So that is why it will not work.

I am currently using the same alternative you are by having my test read credentials from a file.



回答2:

I tested and it all works. what needs to be done is: Source: blazemeter.com how-use-junit-jmeter

1) add the following jar files in the project libraries:

..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_core.jar

..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_junit.jar

2) in the class, add the reference to the sampler:

import org.apache.jmeter.protocol.java.sampler.JUnitSampler;

3) Add the code in the @Test tag to handle variables: Click to see Code

4) Define variables in JMeter (both in user define variables and CSV)

Click to see JMeter Config

Hope it helps!



标签: junit jmeter