Collection, no explanation. Implementation and design of JMeter interface test database assertion

Time:2022-7-23

Most interface tests use the response value of the interface as the basis for interface verification, but in the interface that interacts with the database, this result judgment is not enough to judge the correctness of the interface. This paper will use JMeter as the tool of interface test to realize the database assertion of the interface

1、 General idea overview

  • Initiate interface requests using JMeter
  • Use JSON extractor to get the key content in the interface response result
  • Use JMeter to initiate database operation and get the value of the database
  • Use BeanShell assertion to realize the comparison between interface response content and database content

2、 JMeter initiates interface request

Taking the search interface as an example, the business requirement is to search for goods through keywords. Interface information is as follows:

Interface information:

Input parameters:

Return result:

Add HTTP request in JMeter according to the above interface information, as shown in the following figure

3、 Add JSON extractor for interface request

The JSON extractor is added to obtain all the names in the interface response, which is used to compare one by one with the name value found in the database

1. Add JSON extractor

Select search request — add — post processor — JSON extractor

2. Set JSON extractor

 

Explanation of various parameters:

1. Names of created variables: refers to the variables stored in the content obtained by the JSON extractor from the response. It can be defined casually as long as there is no conflict. It is recommended to give a meaningful name instead of ABC meaningless name

2. JSON path expressions: the expression extracted by JSON indicates that the content needs to be matched from the response according to this rule. The expression in the figure indicates the corresponding values of all name fields in the matching response

3. Match No. (0 for random): the policy of storing the matching results into variables. By default, if nothing is written, it is 0

  • 0 means random in a stored variable. For example, if we match the values of multiple name fields, one of the values will be randomly assigned to the variable specified by names of created variables
  • -1 means to use a numeric suffix to identify each matching result. For example, if we match the values of multiple name fields, then eventually each result will correspond to a variable, and the variable name is name_ 1、name_ 2、name_ 3、name_ 4 ….
  • X means to assign the value of the second match to the variable specified by names of created variables. For example, if you write 2, the value of the name field of the second match will be assigned to name
  • Compute concatenation var: indicates that if multiple values are matched, multiple values can be stored in a variable. The variable name is the variable name specified by names of created variables plus_ All, for example, name in the current figure_ All, multiple stored values are divided by
  • Default values: indicates the default value of the variable if the JSON expression does not match any value

4、 Configure database to initiate database request

Take MySQL database as an example

1. Configure database connection components

To configure the jar package required by the database, Download MySQL connector java.5.1.46.jar, which can be downloaded directly on Maven. The download address ishttps://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.46

After downloading, put it into the lib/ext directory under the JMeter directory, and then restart JMeter

Configure JDBC components, select thread group — add — configure components — JDBC connection configuration

 

Core field meaning

  • Variable name for created pool: indicates the name of the connection configuration, which will be used in subsequent JDBC requests
  • Dabase URL: indicates the connection address of the database instance
  • Jdbc driver class: indicates the connection driver class
  • Username: indicates the user name of the database instance
  • Password: indicates the password to connect to the database instance

2. Database request, please note that this request needs to be placed before the query interface request

Select thread group — add — sampler — JDBC request

Core field meaning

  • Variable name of pool declared in JDBC connect configuration: indicates the configuration name of the database to be connected, which is consistent with the above configuration
  • Query: fill in the SQL statement. Here, we are querying the commodity table, fuzzy query through the name field, and only the name field is returned
  • Result variable name: indicates the variable in which the content found in the database is stored. Here is the variable name

5、 Script debugging

1. Add a debugging sampler to check whether the value of each variable is correct

Select thread group — add — sampler — debug sampler

2. Add view result tree to view request results

Select thread group — add — listener — view the result tree

3. Execute the test to check whether the values of each variable are correct

6、 Use BeanShell assertion to complete the verification of interface results and database results

Select search request — add — assert — BeanShell assert

Execute the whole script, and the execution is passed. The full picture of the script is as follows

 

Recommendation of previous technical articles

Interface test (1): sedimentation! Stay up late and sort out 100 knowledge points of interface testing!! Professional literacy!

Interface test (2): ❤️ This should be the most detailed Chinese tutorial for postman ❤️ (novice use, simple and clear)

Interface test(3): write an interface test tool casually to show my ability to my younger sister