企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] <br> ### 一般项目结构 * Collection 项目(测试集) * Folder 模块 * request1 请求1 * request2 请求2 ### Collection 介绍 我们可以称之为‘测试骨架’或‘测试集’。从工具外观上,像是一个文件夹,`用于组织和管理接口用例`相关信息。实际上它是一个可执行文件,是所有API和Postman内置工具的核心。是所有内置工具的`执行的开始`,包括:MOCK、文档、测试、监测、发布。 ![](https://box.kancloud.cn/402d0a51ae5cb5c7d942bde79588177e_539x413.jpg) ### 创建 Collection ![](https://box.kancloud.cn/ace0696f0c182ee44755b4051a85ebd2_402x269.jpg) ![](https://box.kancloud.cn/b44c98dde9260766bc09898f36f3d2e5_798x692.jpg) - 测试集名称:一般可以采用项目名称 - 描述文档:描述项目的信息 - 前置脚本:在这个测试集下的每个接口,请求发送之前执行的脚本,一般用于参数构造处理 - 断言测试:在这个测试集下的每个接口响应后,都会执行的断言 - 变量设置:在这个测试集下的所有接口请求,都可以共享的变量 ### 分享 Collection 当我们需要与其他团队成员分享Collection时,我们有多钟分享的形式,下面只介绍几种常见的。 - 通过分享URL,使其他成员可以通过URL能访问到Collection ![](https://box.kancloud.cn/78afa00c1ec6d341a2047243b0a8fad7_420x293.jpg) ![](https://box.kancloud.cn/8efd6564662f6376d2e5e139d4f5a144_518x206.jpg) - 通过注册账号,共享项目 - 通过导出Collection文件,实现传播 ![](https://box.kancloud.cn/5a6e7ecb0daf705ae8a8fe9074be83b3_431x312.jpg) ![](https://box.kancloud.cn/a90f22a6ca8fc296313f75af36a5fa19_482x309.jpg) 这里导出到`D://tmp.postman_collection.json`,下面命令行执行会用到。 ### 运行 Collection 当我们运行一个Collection后,在Colection中的requests会逐一顺序执行,这在我们做API自动化测试中,起着非常好的作用。 #### 通过app中的“Runner”按钮运行 ![](https://box.kancloud.cn/1ea575156bca3df6a174bd2e8d5cab37_555x78.jpg) ![](https://box.kancloud.cn/c59626798ea2be3f57a84d0a7bff5736_501x793.jpg) 运行结果报告如下: ![](https://box.kancloud.cn/08a558b6c20f25b3eadc4043db87bd97_1267x346.jpg) #### 通过 `Newman` 命令行工具运行 Newman 是一个Collection Runner命令行工具,它是建立在Node.js上的,运行Newman之前,需确保我们已经安装了Node.js 安装后Node.js后,通过npm进行Newman安装 ```cmd npm install -g newman ``` **通过Collection 文件运行**,命令行运行命令如 ```cmd $ newman run D://tmp.postman_collection.json ``` 运行结果如下: ```cmd newman tmp → postman-echo.com/post POST postman-echo.com/post [200 OK, 642B, 739ms] → postman-echo.com/get?foo1=bar1&foo2=bar2 GET postman-echo.com/get?foo1=bar1&foo2=bar2 [200 OK, 539B, 238ms] ┌─────────────────────────┬──────────┬──────────┐ │ │ executed │ failed │ ├─────────────────────────┼──────────┼──────────┤ │ iterations │ 1 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ requests │ 2 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ test-scripts │ 0 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ prerequest-scripts │ 0 │ 0 │ ├─────────────────────────┼──────────┼──────────┤ │ assertions │ 0 │ 0 │ ├─────────────────────────┴──────────┴──────────┤ │ total run duration: 1049ms │ ├───────────────────────────────────────────────┤ │ total data received: 553B (approx) │ ├───────────────────────────────────────────────┤ │ average response time: 488ms │ └───────────────────────────────────────────────┘ ``` **通过Collection分享URL运行**,格式如: ```cmd $ newman run https://www.getpostman.com/collections/cb208e7e64056f5294e5 ``` **更多newman参数** ```cmd $ newman run -h ``` 参数如下: ```cmd Options: Utility: -h, --help output usage information -v, --version output the version number Basic setup: --folder [folderName] Specify a single folder to run from a collection. -e, --environment [file|URL] Specify a Postman environment as a JSON [file] -d, --data [file] Specify a data file to use either json or csv -g, --globals [file] Specify a Postman globals file as JSON [file] -n, --iteration-count [number] Define the number of iterations to run Request options: --delay-request [number] Specify a delay (in ms) between requests [number] --timeout-request [number] Specify a request timeout (in ms) for a request Misc.: --bail Stops the runner when a test case fails --silent Disable terminal output --no-color Disable colored output -k, --insecure Disable strict ssl -x, --suppress-exit-code Continue running tests even after a failure, but exit with code=0 --ignore-redirects Disable automatic following of 3XX responses ``` <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/331f659e8e6cddb0d9f182e00e32803f_258x258.jpg) ***微信扫一扫,关注“python测试开发圈”,获取更多测试开发分享!***