If you are already using JMeter for interface testing or performance testing at work, you may run into the problem that jMeter variable values cannot be passed across thread groups. Test interview Guide

Look, the official explanation has already been given. This is not a defect of JMeter, it is the design of JMeter. If the variables are already identified before starting the test, we can use parameterized associations to do so. But what if the values are generated dynamically, changing each time we loop through them?

At this point, you will probably go to Baidu, right? You’ll notice, oddly enough, that all baidu results will consistently tell you to use Beanshell*** and write some unintelligible code. If you don’t know Java, and you’re writing code all this time, and you’re writing obscure stuff, you’re starting to have a headache.

If so, then you continue to read, I will teach you the two. Both ways, you don’t have to write code that doesn’t understand anything.

Method 1: Define the attribute method

In JMeter, properties are global and can be set dynamically. Variables are unique to each thread

  • Step 1: We add two thread groups in JMeter
  • Step 2: In the first thread group, call an interface to extract the value you want and store it in a variable
  • Step 3: In the first thread group, add a Beanshell post-processor, and then open the Tools -> Function Helper Dialog in the menu bar. In the popup window, select the __setProperty() Function. The first value of the expression is the name of the property to be stored. The second value is the name of the variable you defined in step 2. Copy the function, close the popover, and paste the function into the Script window of the BeanShell’s back processor.

  • Step 4: In the second thread group, Add a ‘user parameter’, Add variable, set the variable name, open the function helper again, select the __P function, enter ${third set property name} in the function’s first value text box, generate function, copy function, close popover, Paste it into the value text box of the added variable

  • Step 5: In the second thread group, the interface parameters use the defined variable ${variable name}

In this way, parameters are passed across thread groups. Isn’t it? It’s easy. It’s so easy!

** Note: ** If you want to set multiple properties, you can paste multiple functions in the third step, modify the property name and variable name of the function, each function with English ‘; ‘apart.

If this is too much for you, don’t get discouraged. Read on and I’ll show you a more understandable way. Want to get more practical, easy to use test skills, welcome to pay attention to the lemon class public account, Tencent class search lemon class

Method 2: File transfer method

** One thread group runs the result and stores it to a file, and another thread group reads the file through CSV and extracts the required values from the file as variables

  • Step 1: In JMeter, add two thread groups
  • Step 2: In the first thread group, invoke the interface, then add the monitor -> Save the response to the file, and set the path to save the file and the filename prefix

  • Step 3: Add a CSV data file (CSV set data config) in the second thread group, and set the file name to the saved file path set in the second step. Variable name defined by oneself; Set the separator to ‘\t'(why is this place used \t?

  • Step 4: Add the regular expression extractor (or JSON extractor), apply to select ‘Jmeter variable Name to use’, then input CSV to read the variable Name set in the file. Then write the regular extraction

  • Step 5: In the second thread group, add the interface, using the variable names from the re extractor

At this point, file transfer method is complete.

Isn’t it that both methods are fairly simple, and neither of them has to write arcane code! In addition, the parameter values are passed across thread groups.