JMeter usage problem: store interface return variables as csv files


When JMeter is used for interface testing, there are tests of multiple threads and interfaces in the whole jmx test plan. However, interfaces can be classified, such as business interface, query interface, update interface, etc.

Considering that the automatic interface test is generally one-time and has a complete closed-loop link, the general steps should be as follows. Of course, it may be complicated, such as the conversion of interface parameter format, the generation of request header, etc

Configure test environment - > create test data - > clean up database test data - > call interface test - > database query result data (assertion) - > clean up database test data - > restore test environment - > generate test report - > send test email.

So, it's troublesome to make test data every time and delete it after running?

In order to be lazy, I thought that some important fields in the returned data of the business interface can be stored and used as input parameters of the query interface. The automatic interface test cases of the general business interface are very perfect. In this way, the query interface can not only greatly cover all situations, but also further verify whether the call of the business interface is correct, killing two birds with one stone.

Of course, JMeter itself has mathematical functions, which can set global variables across threads. As long as the regular fetching interface return value is configured as global variables, it can be invoked in other threads theoretically.

However, if the returns of multiple threads are recorded, there will be a lot of global. Therefore, in order to be more flexible, I wrote my own java method and called it as a jar package using beanshell.

In my scenario, there are multiple business interfaces, including recharge, payment, refund and so on. If the transaction is successful, the order number will be returned, and then I have an interface to query business information according to the order number, that is, the order number returned by the interface of my business interface automation test will be used as an input to my query interface.

First, we need to reference our own jar package in JMeter:

The grab interface returns, and the beanshell code stored in the txt file is as follows:

import writeFile.*;
System.out.println("******write file*************");
String filePath = "D:/apache-jmeter-3.2/work_txt/order_no.txt";
String content = vars.get("orderNo_pay_gua");   //The variable here is the variable name obtained by regular expression sampling from the sample interface, which can be modified according to the actual situation.
AppendFile.appendFile(filePath,content);

After the whole test is executed, of course, we need to empty our txt file every time. The beanshell emptying code is as follows:

import writeFile.*;
System.out.println("******Clear file*************");
String filePath = "D:/apache-jmeter-3.2/work_txt/order_no.txt";
AppendFile.clearFile(filePath);
 

Attach the jar package source code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class AppendFile {
//Write content to the specified file fileName
public static void appendFile(String fileName, String content) throws IOException{
FileWriter writer = null;
try {

writer = new FileWriter(fileName, true);
content = content + "\n";
writer.write(content);
} catch(IOException e){
e.printStackTrace();
}finally{
if (writer != null){
writer.close();
}
}
}
//Empty contents in fileName
public static void clearFile(String fileName) throws IOException{
FileWriter writer = null;
try{

writer = new FileWriter(fileName);
writer.write("");
} catch(IOException e){
e.printStackTrace();
}finally{
if (writer != null){
writer.close();
}
}
}
}

Download address of jar package: https://pan.baidu.com/s/1mmDa1yXKHNE9phfklHL8fQ

Extraction code: see the official account: the sad hot strip is self - access.

Now there is such an opportunity. I invite you to join our software testing learning exchange group: 914172719. Note "csdn". You can discuss and exchange software testing together, learn software testing technology, interview and other aspects of software testing together, and have free live classes to gain more testing skills. Let's advance Python automated testing / test development together, The road to high pay.

Let's give you a word of encouragement: when our ability is insufficient, the first thing to do is internal practice! When our ability is strong enough, we can look outside!

Finally, a supporting learning resource is prepared for you, You can be in the official account: [sad hot note] get a 216 page interview classic document of Software Test Engineer for free. And share the corresponding video learning tutorials for free! The materials include basic knowledge, Linux essentials, Shell, Internet program principle, Mysql database, special topics of packet capturing tools, interface testing tools, advanced testing Python programming, Web automation testing and APP automation Automated testing, interface automation testing, advanced continuous integration testing, test architecture development, test framework, performance testing, security testing, etc.

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Another software testing post, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer should be enclosed in double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

As a test engineer with 1 year working experience, my suggestions before the interview are as follows

"After a year in office, the automation software test hired by high salary was persuaded to quit."

4 months of self-study software testing into Ali! How to move from functional testing to automation... What have I experienced

6000 yuan applied for the training course. Three months later, I successfully "cheated" into Tencent's big factory with a monthly salary of 15000 yuan

Keywords: Java Programmer software testing IT Stress testing

Added by CNibbana on Sun, 02 Jan 2022 20:44:14 +0200