A powerful tool for automating scene design based on the Postman interface

Design postman automation scenarios using Xmind or Yaml

The introduction

Postman is a relatively lightweight interface testing tool that performs well on individual interfaces. In terms of batch testing interface, Runner Collections is provided. Although it can be used for process testing, it is not very convenient for management. Such as: Create one collection in Postman for the interface document, then create another collection for the scenario test, then select the interface from the interface document collection and copy it into the scenario test collection, and may share the same interface in different scenarios. This method sounds good, but when the version of the interface is upgraded, you need to find all the corresponding interfaces in the scene to modify, which will be troublesome to manage.

concept

This tool generates a collection of scenario tests from the collection of interface documents based on the scenario flow written by Xmind or Yaml, which is quite convenient and only needs to be rebuilt once even if the version is upgraded. It also provides some convenient setting methods and assertions.

What can do

  1. Convenient management. Only the collection and Xmind/Yaml scripts for managing interface documents are required.
  2. The scene flow is more intuitive. On Xmind/Yaml you can see the entire process visually, as well as the parameter definitions and assertions for each interface in detail.
  3. Simplify code writing. In the testing industry, poor coding is common, and while testing does not require particularly strong coding skills, Postman does provide some shortcuts in the Tests interface, but they are not simplified enough or comprehensive, such as signing request parameters.
  4. Provide some quick functions. Postman has a few built-in functions, such as randomly generating 32 UUID, MD5, getting the current time, getting the last 7 days, the last 30 days, parameter signing, and so on, all of which require manual coding.
  5. No dependency. This tool is just a script into Postman script tools, even if not later, you can maintain Postman scripts. .

Address: Code Cloud Address: Github

Let’s take a look at the renderings first

How to get started

Install PostScene using PIP

pip install -U PostScene
Copy the code

call

from post_scene.post_scene import PostScene

yaml_path = './yaml/demo.yaml'  The path to the script file
xmind_path = './xmind/demo.xmind'  The path to the script file
api_document_path = './api_document/demo.postman_collection.json'  # postman Json data file path
api_document_url = 'https://www.getpostman.com/collections/ Mosaic Mosaic Mosaic Mosaic'  You can also use Postman's share link


# PostScene.covert(yaml_path, api_document_path,scene_dirs='./scene')
# PostScene.covert(xmind_path, api_document_path,scene_dirs='./scene')
PostScene.covert(yaml_path, api_document_path,scene_dirs='./scene')
Copy the code

example


  • Create a new file. It doesn’t matter what the name is, but for the sake of iterative development, it’s best to add a version number.

Demo – scenev1.0. Yaml

  • scripting
name: Demo - scenev1.0                                         # the name of the collection
scene:
   name: Place the order process                                             The name of the collection folder
   scene:
     Login:                                                   #API interface name
       pre:                                                 Interface pre-request script
         sign:                                              # parameter signature
           secret: 1850e165f1fc19420f2ba3d3a1a5ffe4
         set:                                               Set the variable value
           userName: user
           password: user123
           time: ? times                                    Get the present time
           onceToken: ? uuid32                              Generate a 32-bit UUID
       tests:                                               Post request script
         assert:                                            Postrequest assertion
           express:
             content: $json.data.code = = = '1'               Return json data with code equal to 1
             set:                                           # assert that token and UID data were saved successfully
               token: $json.data.token
               uid: $json.data.uid
     Search restaurants by restaurant name:
       pre:
         sign:
           secret: 1850e165f1fc19420f2ba3d3a1a5ffe4
         set:
           canteenName: Like tea
       tests:
         assert:
           expect:                                         # assert that the name of every object in the returned canteenList contains happy tea
             content: $json.data.canteenList
             item: $it.name                                 
             include: Like tea
             set:
               canteenId: ? find(json.data.canteenList, it.canteenName = = 'xi tea GO').canteenId  # Get the CanteenId of Happy Tea Go
     Search for products by product name:
       pre:
         sign:
           secret: 1850e165f1fc19420f2ba3d3a1a5ffe4
         ref: canteenId
         set:
           goodsName: Oreo melaleuca
       tests:
         assert:
           expect:
             content: $json.data.goodsList
             item: $item.name
             include: Oreo melaleuca
             set:
               goodsId: ? find(json.data.goodsList, it.goodsName = = 'Oreo layer').goodsId
     Add to shopping cart:
       pre:
         sign:
           secret: 1850e165f1fc19420f2ba3d3a1a5ffe4
         ref: goodsId
         set:
           count: 1
       tests:
         assert:
           express:
             content: $json.code = = = '1'
             set:
               pocketId: $json.data.pocketId
.

Copy the code
  • script
  1. Download the project using Git or a browser and open it with PyCharm.

  2. Select the API documentation collection you have prepared in Postman and export it. This is exported as demo.postman_collection.json

  1. The API_document script puts the exported document into the project into XMIND or YAML

  1. The generated scene file is placed in the SRC /scene folder and imported using postman’s import

If you’re familiar with Postman scripts, the following should be accessible to you. It doesn’t matter if you are not familiar with it. As long as you follow the script, you can also complete the writing of the script. The original intention of the design is to reduce the threshold. The tutorial is explained by YAML, because it is not very convenient to explain with Xmind, so we need to cut a lot of pictures. For xmind, just look at Demo. Xmind.

Note: Each test case consists of two parts: Pre (before request) and Tests (after request). Pre can be absent, but tests assertions must be present otherwise they are meaningless. The name of each test case must match the interface name of the Collection in the documentation. This is the specification.