> ## Content Index
> Fetch the complete content index at: https://www.richarvin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 使用postman造数
- URL: https://www.richarvin.com/shi-yong-postman-zao-shu/
- Published: 2025-01-05T08:23:26.000Z
- Updated: 2025-01-05T08:23:50.000Z
- Author: Laurawu
- Tags: 编程技术

在日常实际工作中，postman除了可以用于单接口调试及自动化测试之外，还可以作为我们日常造数的工具来使用。在日常测试中，无论是功能测试还是协助开发定位问题，总是绕不开造数。在没有现成的造数平台可用的情况下，有什么快捷的方法可以帮我造数呢?接下来我们讲下如何使用postman来帮我们造测试数据，从而提升我们的工作效率。

Postman的运行时基于Node.js。因此你可以使用 Java script 语言在postman的 pre-script 模块 或者 post-response 模块编写代码，来实现我们想要的各种逻辑。  
在 Conllections 和 request 中都有 pre-script 和 post-response，在接口运行过层中各模块执行的先后顺序为：  
Conllections pre-script-->request pre-script-->request-->request post-response-->Conllections post-response

**Conllections pre-script**：在文件夹下的任意请求发送之前执行，直接运行文件夹时，只会在第一个request发送之前调用

**Conllections post-response**：在文件夹下的任意请求发送之后执行，直接运行文件夹，则只会在第一个request发送之前调用

**request pre-script**：在当前请求发送之前执行

**request post-response**：在当前请求发送之前执行

**1）单接口造数**  
例如系统中一个新建学校的接口，接口对 学校名称做了唯一校验。  
现在需要调用这个接口 新建10个学校。  
我们可以在request的pre-request 模块编写如下代码来实下：

```java
var randNum =  new Date().toString().substr(7, 13);
var parentSchoolName = pm.collectionVariables.get("schoolNameString") + "的学校" + randNum;
var parentSchoolEnName = pm.collectionVariables.get("parentSchoolEnNameString") + randNum;
pm.collectionVariables.set("randNum", randNum);
pm.collectionVariables.set("parentSchoolName", parentSchoolName);
pm.collectionVariables.set("parentSchoolEnName", parentSchoolEnName);
```

![](https://picture.richarvin.com/202501/20250104001.png)

![](https://picture.richarvin.com/202501/20250104002.png)

如果想要一次生成10个学校，则可以用过右键 Collections或则文件夹选择 run Collection 或者 run floder 勾选需要我们写好的接口，在Iteration中输入10 点击运行，就可以将对应接口运行10次。

**run conllection**

![](https://picture.richarvin.com/202501/20250104003.png)

**run floder**

![](https://picture.richarvin.com/202501/20250104004.png)

**在run flode中运行指定request 10次**

![](https://picture.richarvin.com/202501/20250104005.png)

**调用结果查看**

![](https://picture.richarvin.com/202501/20250104006.png)

---