Best practices for automated testing of JMeter interfaces

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 various scenarios, and discover interface performance issues. Below are some steps and techniques for automated testing of JMeter interfaces.

step

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 the test plan, the tester needs 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

Sampler 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 jsonin 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":

Make assertions about HTTP response codes:

JSON assertion

Add 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 the data returned by the interface:

The JSON Path above refers to the JSON path expression. The expression consists of a series of attribute 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"
            ]
        }
    ]
}

The corresponding value can be obtained using the following expression:

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

Assert result

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

Listener is another important component in JMeter testing, which is 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:

Skill

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

JMeter has many 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. Plug-in Manager is a tool for installing and upgrading JMeter plug-ins. 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 at the same time, 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 automated interface 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: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves [guaranteed 100% free]

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/132839242