Jmeter入门使用2 - 接口自动化

需求:管理后台有个商品保存并上架功能,当我们需要建多个商品时,利用接口自动化实现更方便;商品分为两种,一种有规格,一种无规格;实现方式如下:

Jmeter入门使用2 - 接口自动化

Step1-添加元素

1、test plan添加参数,存放token值,token可以在多个thread groups中使用。

Jmeter入门使用2 - 接口自动化

 2、添加相关元素

  • http request defaults(设置服务器地址);
  • http header manager(设置请求头部信息);
  • counter(产生自增变量):

The counter config lets the user configure a starting point, a maximum, and the increment. The counter will loop from the start to the max, and then start over with the start, continuing on like that until the test is ended

  • 4个requests:

保存有规格商品(添加JSON Extractor,提取商品spu_id,保存到SpuID)

发布有规格商品(使用参数${SpuID})(添加JSON Assertion,判断是否上架成功)

保存无规格商品(添加JSON Extractor,提取商品spu_id,保存到SpuID)

发布无规格商品(使用参数${SpuID})(添加JSON Assertion,判断是否上架成功)

  • view result tree: 查看结果

Jmeter入门使用2 - 接口自动化

counter设置一个开始值,在每个循环中增加1

Jmeter入门使用2 - 接口自动化 

 保存商品的request,需用到counter中的变量${cnt}来改变商品名称

Jmeter入门使用2 - 接口自动化

Step2-设置循环

Jmeter入门使用2 - 接口自动化

Step3-跑接口

 Jmeter入门使用2 - 接口自动化

查看管理后台数据

Jmeter入门使用2 - 接口自动化

the end!!!