Stack Reduce
单元测试

JMeter

JMeter 接口测试指南。

安装

1. 下载

官方下载

下载 Binaries 下的 apache-jmeter-x.x.zip,解压即可使用。需要预装 JDK 8+。

2. 启动

  • Windows:双击 bin/jmeter.bat
  • Linux / macOS:执行 bin/jmeter.sh

3. 切换中文

菜单 OptionsChoose LanguageChinese (Simplified)

创建接口测试

1. 添加线程组

右键 Test PlanAddThreadsThread Group

参数设置
Number of Threads1(接口测试设为 1)
Ramp-Up Period0
Loop Count1

2. 添加 HTTP 请求

右键 Thread GroupAddSamplerHTTP Request

参数示例
Protocolhttp
Server Namelocalhost
Port Number8080
MethodGET
Path/api/users/1

3. 添加请求头

POST 请求通常需要设置 Content-Type: application/json

右键 Thread GroupAddConfig ElementHTTP Header Manager

添加:Content-Type = application/json

4. 添加断言

右键 HTTP RequestAddAssertionsResponse Assertion

参数说明
Apply toMain sample only
Field to TestResponse Code
Pattern Matching RulesEquals
Patterns to Test200

也可以添加 JSON Assertion 验证返回字段:

右键 HTTP RequestAddAssertionsJSON Assertion

参数示例
JSON Path$.name
Expected Value张三

5. 添加结果查看器

右键 Thread GroupAddListenerView Results Tree

点击工具栏绿色启动按钮运行测试,在 View Results Tree 中查看请求和响应详情。

参数化

CSV Data Set Config

右键 Thread GroupAddConfig ElementCSV Data Set Config

参数说明
Filename数据文件路径(如 data.csv
Variable Names变量名,逗号分隔(如 id,name
Delimiter分隔符,默认逗号

在 HTTP 请求中使用 ${id}${name} 引用变量。

本页目录