JMeter is an open source software for load testing, performance testing and functional testing. Testers can use JMeter to write test scripts to simulate many different load situations to evaluate the performance and stability of the system. Following are the steps to write a JMeter test script.
Step 1: Create a Test Plan
In JMeter, a test plan is the highest level of testing, which contains various elements and configurations such as thread groups, assertions, listeners, etc. Testers need to create a new test plan in JMeter and add the necessary elements and configurations.
To create a new test plan, choose: 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. The 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 the requirements.
To create a thread group, right-click the test plan and select: Add > Thread (User) > Thread Group.
Fill in the thread group information.
- Number of threads: 10 threads is to simulate 10 users.
- Ramp-Up time (seconds): Thread preparation time. If the number of threads is 10 and the preparation time is 10, it takes 1 second to start 1 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 keep sending requests until manually stopped.
Step 3: Add a sampler
A sampler is a core component of JMeter testing that simulates actions performed by the user. Testers need to add one or more samplers and set their properties. Common sampler types include HTTP requests, TCP requests, FTP requests, JDBC requests, and more.
Here we take adding an HTTP request as an example. To add an HTTP request, right-click on the thread group and select: Add > Sampler > HTTP Request.
Fill in the HTTP call information.
The passed data is in json format, so add header information: Content-Type: application/json. Right-click HTTP Request and select: Add > Config Elements > 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. Testers need 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 header, 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 Response Assertion: Right-click Interface > Add > Assertion > Response Assertion.
Make an assertion that the response text contains the string "Apple":
Make assertions on HTTP response codes:
JSON assertion
Add JSON Assertion: Right click 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 above JSON Path refers to the JSON path expression. An expression consists of a series of property names and array indices: starting with "$.", 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: Gets "John".
- $.cars[0].name: Gets "Ford".
- $.cars[1].models[2]: Get "X5".
Assertion result
Run the use case. If the response matches the assertion, nothing happens. Otherwise, you will see an error message in the result tree listener.
Step 5: Add Listeners
Listener is another important component in JMeter testing, it is used to collect test results and generate reports. Testers need to add one or more listeners and set their properties. Here are some commonly used JMeter listeners:
- Aggregated Reports: Provides information on transaction response times, throughput, and error rates.
- View Results Tree: Displays the response for each request, including request headers, request body, and response body.
- Listener Graphical Results: Visualize test results for easier analysis of performance issues.
- Assertion result: Verifies that a response satisfies a specific condition.
- Distributed Load Test Graph: Shows the load on different servers.
Add a listener: Right-click on 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, view the report
After testers finish configuring the test plan, they can run the test plan and collect test results. Testers can generate reports through the listener and adjust the configuration of the test plan based on the results.
After the use case finishes running, you can see the corresponding results in different configuration monitors.
View the result tree:
Summary report:
Graphical result:
The above are the basic steps of JMeter test script writing. Testers need to flexibly use various functions and components of JMeter according to specific needs and scenarios to write efficient and accurate test scripts.
Try a better API tool than JMeter: Apifox
It is easier to use Apifox as a test script. Here's how to use Apifox as a test script.
create interface
call interface
Click the "Send" button directly to see the return of the interface.
add assertion
Create test cases
Edit the test steps and configure the pressure test parameters.
Run test cases and view reports
JMeter can be used for performance testing, but in daily work, we have more to do: API design, API debugging, API Mock, etc. We may use multiple tools, but frequent switching between multiple tools (API design tool Swagger, API testing tool JMeter) often leads to data inconsistency and inefficient team collaboration.
Apifox is a more intelligent API design/development/testing tool. Compared with JMeter software, Apifox has the following advantages:
easy to use
Apifox provides a simple and easy-to-use interface and interactive methods, and users do not need to write scripts or perform complex configurations to complete API testing. In addition, Apifox also provides free templates and samples to help users create API requests faster.
automated test
Apifox provides the function of automated testing, and users can reduce testing time and labor costs through automated testing. Users only need to set up a test plan, and Apifox can automatically run the test and generate test results and reports. In addition, Apifox also supports test suites, which can run multiple test cases at one time to improve test efficiency.
Multi-environment support
Apifox supports multiple test environments, and users can test different APIs by switching environments. Users only need to set environment variables, and Apifox can automatically switch the URL and parameters of API requests. In addition, Apifox also supports environment import and export, which is convenient for users to share test environments between different devices or teams.
Collaborate and share
Apifox provides collaboration and sharing features, users can share test results and test data with team members or others. Users can also export test results and reports to HTML format. In addition, Apifox also supports team collaboration and authority management, allowing team members to share API design and testing resources.
In short, compared with JMeter, Apifox is easier to use and more intelligent, which can help users to perform API testing and management more efficiently. Whether it is an individual developer or an enterprise team, Apifox can improve the efficiency and quality of API testing. Therefore, more and more people are choosing to use Apifox instead of JMeter.
Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.