Best practices for automated testing of JMeter interfaces (recommended collection)

JMeter is an open source load testing tool that can simulate the load of multiple protocols and applications, including HTTP, FTP, SMTP, JMS ,SOAP and JDBC, etc. When conducting automated interface testing, using JMeter can help us quickly build test cases, simulate multiple scenarios, and discover interface performance issues. Below are some steps and tips for automated testing of JMeter interfaces.

Steps

Step 1: Create a test plan

In JMeter, the test plan is the highest level of testing. It contains various elements and configurations, such as thread groups, Assertions , listeners, etc. Testers need to create a new test plan in JMeter and add necessary elements and configurations.

To create a new test plan, select: File > New > Fill in the plan name.

Step 2: Add thread group

In a Test plan, testers need to add one or more thread groups. Thread group is the basic unit of testing, which defines the number of concurrent users, duration and other properties of the test. Testers need to set the properties of the thread group according to requirements.

To create a thread group, right-click the test plan and select: Add > Threads (User) > Thread Group.

Fill in the thread group information.

  • Number of threads: 10 threads simulate 10 users.
  • Ramp-Up time (seconds): Thread preparation time. If the number of threads is 10 and the preparation time is 10, then it takes 1 second to start one thread.
  • Cycles. If the number of threads is 10 and the number of loops is 10, then each thread sends 10 requests. The total number of requests is 10*10 = 100. If “Forever” is checked, all threads will continue to send requests until manually stopped.

Step 3: Add Sampler

Sample is the core component of JMeter testing, which simulates the actions performed by the user. The tester needs to add one or more samplers and set their properties. Common sampler types include HTTP requests, TCP requests, FTP requests, JDBC requests, etc.

Here we take adding an HTTP request as an example. To add an HTTP request, right-click the thread group and select: Add > Sampler > HTTP Request.

Fill in the HTTP call information.

The data passed is in json format, so header information needs to be added: Content-Type: application/json. Right-click the HTTP request and select: Add > Configuration Element > HTTP Header Management.

Add header information: Content-Type: application/json.

Step 4: Add Assertions

Assertions are an important component in JMeter testing and are used to verify that the server’s response is as expected. The tester needs to add one or more assertions and set their properties.

Common assertion types include response assertions and JSON assertions:

  • Response assertions include: text, response code, response headers, response time, etc.
  • JSON assertions allow users to specify a JSON path expression and verify that the JSON response conforms to that expression.

Response assertion

Add a response assertion: Right-click the interface > Add > Assertion > Response Assertion.

Assert that the response text contains the string “Apple”:

Assert on HTTP response code:

JSON assertion

Add a JSON assertion: Right-click the interface > Add > Assertion > JSON Assertion.

Make an assertion on the total returned by the interface:

Make an assertion on the first item name of data returned by the interface:

?The JSON Path above refers to the JSON path expression. The expression consists of a series of property names and array indexes: starting with “$.” and separated by “.” and “[]”. For example, the following JSON response:

 "name": "John",
    "age": 30,
    "cars": [
        {
            "name": "Ford",
            "models": [
                "Fiesta",
                "Focus",
                "Mustang"
            ]
        },
        {
            "name": "BMW",
            "models": [
                "320",
                "X3",
                "X5"
            ]
        }
    ]
}

You can use the following expression to get the corresponding value:

  • $.name: Get “John”.
  • $.cars[0].name: Get “Ford”.
  • $.cars[1].models[2]: Get “X5”.

Assertion results

Run the use case. If the response matches the assertion, nothing happens. Otherwise, you will see error messages in the result tree listener.

Step 5: Add a listener

Listeners are another important component in JMeter testing, which are used to collect test results and generate reports. The tester needs to add one or more listeners and set their properties. The following are some commonly used JMeter listeners:

  • Aggregated reporting: Provides information on transaction response times, throughput, and error rates.
  • View the result tree: Displays the response for each request, including request headers, request body, and response body.
  • Listener graph results: Visualize test results to make it easier to analyze performance issues.
  • Assert results: Verify that the response meets specific conditions.
  • Distributed load test graph: shows the load on different servers.

Add a listener: Right-click the thread group (or sampler) > Add > Monitor > Specific Monitor. Here we take the most commonly used “view result tree” as an example.

Step 6: Run the test plan and view the report

After the tester completes the configuration of the test plan, he can run the test plan and collect the test results. Testers can generate reports through listeners and adjust the configuration of the test plan based on the results.

After the use case is finished running, you can see the corresponding results in monitors with different configurations.

View the result tree:

Summary report:

Graphical results:

Tips

Using CSV data files

If you need to simulate multiple scenarios, you can use CSV data files. You can save different request parameters in a CSV file and then configure the request parameters using the CSV data file in JMeter. This can quickly generate multiple test cases and improve testing efficiency.

Using JMeter plug-in

There are many JMeter plugins that extend its functionality. For example, you can use the JSON Path Extractor plugin to extract specific fields from the JSON data returned by the interface. You can also use the Response Assertion plug-in to verify whether the results returned by the interface are as expected.

If you need to use a plug-in, you need to follow these steps to install it:

1. Download the plug-in manager

First, you need to download the JMeter plugin manager. Plugin Manager is a tool for installing and upgrading JMeter plugins. You can download the latest version of the plugin manager on the official website.

2. Install the plug-in manager

Copy the downloaded plug-in manager file to the lib/ext directory of JMeter and restart JMeter.

3. Install the plug-in

Open JMeter and select Options -> Plugins Manager. In the Plugins Manager interface, you can search for the plug-in you want to install and click the Install button to install it.

4. Restart JMeter

After completing the installation of the plug-in, you need to restart JMeter for it to take effect.

Use distributed testing

If you need to simulate a large number of concurrent user access interfaces, you can use JMeter’s distributed testing function. You can distribute test plans to multiple JMeter clients, run multiple thread groups simultaneously, and simulate a large number of concurrent user access interfaces. This can improve testing efficiency and discover interface performance issues.

In short, using JMeter for interface automated testing is a fast, efficient, and reliable testing method. Through reasonable configuration and techniques, performance problems of the interface can be discovered and the quality and stability of the application can be improved.

Finally, I would like to thank everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!