search
HomeWeb Front-endJS TutorialAutomated testing using PostMan

Automated testing using PostMan

Apr 03, 2018 am 09:14 AM
postmantestautomation

I have been upgrading an old project recently. The first step is to upgrade the node version from 4.x to 8.x. I am worried that problems may arise in the upgrade, so I need to upgrade the service Interface for verification;
If you manually enter various URLs, human flesh check, one or two is fine, the entire service. . Having dozens of interfaces is a waste of time -.-;
Because it is a pure interface service project, we plan to conduct a wave of automated testing for the corresponding API;
So we started looking for the corresponding tools. Suddenly I discovered that the PostMan I usually use seems to support writing test cases -.-, so I followed the documentation and read it;
I was very excited all afternoon. I used ## before. #PostMan is limited to modifying Header and adding Body to send requests. I have never considered using PostMan for testing. After using it for an afternoon, I feel that the New World.
Installation of PostMan

It seems that downloading and using

PostMan must be done through the firewall-.-Because two forms of App are now available:

  1. chrome plug-in (already almost abandoned, it is recommended to use an independent App)

  2. Independent App

And you need to log in to your account when using it. I log in directly with my

Google account-. -There seems to be other ways, but I didn't try them.

Independent App version cloud disk address (

Mac version, 6.0.10 just downloaded today, please get it yourself if you need it): Link: https://pan.baidu. com/s/18CDp... Password:
mrpf

After downloading and decompressing, you can run it directly, and then register an account. The visual account number is mainly used for follow-up. Required for group sharing (you can directly share your call records with others).

Send a request

This is the most basic usage of

PostMan, used to send a request. You can set
Header, Body and other information.
Automated testing using PostMan

Collections

We can save each request sent so that the next time we request the interface, we can call it directly,

If you save the request, it will be saved in a
Collections, similar to a collection.
PostMan provides a method to run all requests in the entire Collections with one click.
Automated testing using PostMan
Automated testing using PostMan

Then we can directly run all the requests in the collection when needed.


Automated testing using PostMan

When saving the request record, select the corresponding

Collection below
Automated testing using PostMan

Start API test

Test script location

Automated testing using PostMan##PostMan
Test script written for the request, At this location, the JavaScript syntax is used, and on the right are some preconfigured code snippets. And we can write a script in Pre-request Script
to execute before sending the request. Some simple syntax

PostMan

also provides an assertion to help with some verification. <pre class="brush:php;toolbar:false">tests['Status code is 200'] = responseCode.code === 200 tests['Data length &gt;= 10'] = JSON.parse(responseBody).data.length &gt;= 10</pre> Assigned a value of

true

means passing, false means failure. The direct assignment of tests
is relatively limited. If you perform some other asynchronous operations in the script, you need to use pm.test. <pre class='brush:php;toolbar:false;'>setTimeout(() =&gt; { pm.test(&quot;test check&quot;, function () { pm.expect(false).to.be.true }) })</pre> Only the above

tests

assignment +pm.test/pm.expect can meet our needs, and the rest are just above this It's just syntactic sugar. Various syntax examples
Send a request in the test script

We can send some new requests based on the result after getting a

API

return result , and then add assertions. <pre class='brush:php;toolbar:false;'>let responseJSON = JSON.parse(responseBody) // 获取关注的第一个用户,并请求他的用户信息 pm.sendRequest(responseJSON[0].url, function (err, response) { let responseJSON = response.json() pm.test(&amp;#39;has email&amp;#39;, function () { pm.expect(responseJSON.email).is.be.true // 如果用户email不存在,断言则会失败 }) });</pre>If we have some dynamic interfaces to test, we can try this writing method.

The first-level interface returns

List
The second-level interface obtains the corresponding information based on the ID
of List. <h3 id="如何处理大量重复的断言逻辑">如何处理大量重复的断言逻辑</h3> <p>针对单个API,去编写对应的断言脚本,这个是没有什么问题的。<br>但是如果是针对一个项目的所有<code>API去编写,类似于判断statusCode这样的断言就会显得很溶于,所以PostMan也考虑到了这点。
在我们创建的Collection以及下层的文件夹中,我们可以直接编写针对这个目录下的所有请求的断言脚本。
Automated testing using PostMan
Automated testing using PostMan
这里的脚本会作用于目录下所有的请求。
这样我们就可以将一些通用性的断言挪到这里了,在每个请求的Tests下编写针对性的断言脚本。

变量的使用

PostMan提供了两种变量使用,一个是global,一个是environment

global

代码操作的方式:

pm.globals.set("variable_key", "variable_value") // set variable
pm.globals.get("variable_key") // get variable
pm.globals.unset("variable_key") // remove variable

通过GUI设置:
Automated testing using PostMan
Automated testing using PostMan

设置完后我们就可以这样使用了:
Automated testing using PostMan

基本上在所有的可输入的地方,我们都能够使用这些变量。

environment

环境变量,这个是权重比global要高一些的变量,是针对某些环境来进行设置的值。
操作方式类似。

在使用代码操作的方式时,只需将globals替换为environment即可。
在发起一个请求,或者一键发送所有请求时,我们可以勾选对应的环境,来使用不同的变量。
Automated testing using PostMan

在针对大量API测试时,拿environment来设置一个domain将是一个不错的选择。
这样在请求中我们只需这样写即可:

{{domain}}/res1
{{domain}}/res2

domain: https://api.github.com

一个简单的示例:

通过直接运行一个Collection,我们可以很直观的看到所有的接口验证情况。
Automated testing using PostMan
Automated testing using PostMan

参考资料

https://www.getpostman.com/do...

之前使用PostMan,最多就是模拟一下POST请求,最近刚好碰到类似的需求,发现原来PostMan还可以做的更多。
这篇只是使用PostMan进行API测试的最基础操作,还有一些功能目前我并没有用到,例如集成测试、生成API文档之类的。

接口相当于是获取和操作服务资源的方式,肯定属于产品的核心。
所以测试是必须的,在交付QA同学之前,自己进行一遍测试,想必一定能节省一部分的时间。

相关推荐:

chrome插件postman安装问题_html/css_WEB-ITnose

The above is the detailed content of Automated testing using PostMan. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools