Interface automated testing - Postman+Newman+Git+Jenkins combat integration (detailed)


foreword

1、Postman

Create a Collection and create an interface request in the Collection, as shown in the figure below.

A1

Write the assertion Test and Pre-request Script corresponding to the interface, as shown in the figure below.

A2

Configure the environment variable or global variable corresponding to the interface.

A3

Export the interface collection Collection and export the environment variables of the corresponding interface, and the export will generate a JSON file.

A4

I have written postman interface test articles before, you can refer to: https://blog.csdn.net/shuang_waiwai/article/details/122215241

2、Newman

newman is Postman's command collection runner, through which you can run and test Postman directly from the command line for subsequent builds on the continuous integration server.

Newman is a nodejs library launched by Postman. Directly speaking, it is a plug-in that Postman's json file can execute on the command line.

The command to install newman globally on Taobao Mirror:

npm install -g newman --registry=https://registry.npm.taobao.org

The installation is successful, as shown in the figure below, showing the version number successfully installed by newman.

A5

To verify whether newman is installed successfully, check the newman version number command: newman -version

A6

Common Newman syntax:

Run the command newman run URL connection or newman run collection filename
-e, - environment <path>: Specify the URL or path of the Postman environment.
-g, --globals: specify the file address or url of the global variable
-d, --iteration-data: specify the data source file path for iteration
-n, --iteration-count: specify the number of iterations
--export-globals: export the global Environment variable
–timeout (ms): Set the time for the entire collection to complete the execution
–export-collection: Export the collection file
–timeport-request (ms): Specify the time to wait for the request to return a response
–timeout-script (ms): Specify the waiting script execution completion time

We can view the relevant parameters that newman run can bring when running through the cmd terminal. newman run -h to view all relevant parameter syntax, as shown in the figure below.

A7

Newman example:

The newman run command allows you to specify a collection to run. You can easily export Postman Collection as json file from Postman App and run it with Newman.

newman run examples/sample-collection.json

Example: Run the interface collection and environment variables exported by Postman, open cmd, navigate to the Json file directory, and enter newman to run the command.

newman run Test.postman_collection.json -e mukeOnline.postman_environment.json

run Run the command of the interface collection, -e brings in the environment variable of the interface collection

We navigate to the JSON file directory of the interface collection and environment variables in the cmd terminal, and the result of running the command line is as follows:

A8

From the above operation, it can be seen that the result of running on the cmd terminal is not very convenient for testers to view and form test reports. So how do we use the newman command to generate an output test report file?

Specify the export format of the test report:
currently supports four formats: cli, json, html, junit

--reporter-{
    
    {
    
    reporter-name}}-{
    
    {
    
    reporter-option}}

Support to export multiple test report formats at the same time

JSON --reporter-json-export <path>

HTML --reporter-html-export <path>

JUNIT/XML --reporter-junit-export <path>

Let's also take the above collection and environment variables as an example. When generating an HTML report, you need to install the html package, and execute it on the command line:

npm install -g newman-reporter-html --registry=https://registry.npm.taobao.org

Install newman-reporter-html, as shown below

A9

Run the command to view the generated test report in the form of an HTML file in the file directory.

newman run Test.postman_collection.json -e mukeOnline.postman_environment.json -r html --reporter-html-export report.html

The generated HTML test report file is shown in the figure below.

A10

We can see that the report style generated by the above HTML is relatively simple, how to make our report look tall? Then we have to use the Htmlextra suite.

First, install and use the htmlextra suite to generate more advanced html reports. The method of use is the same as that of html. You need to install htmlextra, and enter in the command line:

npm install -g newman-reporter-htmlextra

The installation is successful, as shown in the figure below.

A11

Let's execute the above interface collection use case again to see how the test report works. newman runs the command as follows.

newman run Test.postman_collection.json -e mukeOnline.postman_environment.json -r htmlextra --reporter-html-export htmlReport.html

The results of the test report are shown in the figure below. It can be seen that the style of the report is much better than before.

A12

3、Git

Download GitExtensions and Git, upload the json file exported by Postman to github, and pull it down for iteration. Not too much explanation here, you can find the information by yourself.

A13

4、Jenkins

Jenkins is an open source, scalable continuous integration, delivery, deployment (software/code compilation, packaging, deployment) platform based on web interface. Allows continuous integration and continuous delivery projects, regardless of the platform used, can handle any type of build or continuous integration.

Download Jenkins address: https://www.jenkins.io/download/ , download and install.

I wrote an article before: https://blog.csdn.net/shuang_waiwai/article/details/120707562

5. Jenkins configures Git and Newman

Create a new Item and select Freestyle project

A14

The Git address, certificate and branch related configuration of the task

A15

set build newman run command

newman run Test.postman_collection.json -e mukeOnline.postman_environment.json

A16

Save after setting, click Build Now

A17

Generate build results

A18

After the build, a Junit report is generated, an empty xml file is created on Git, and the nweman run command points to the xml file. The command to run before building is modified to

newman run Test.postman_collection.json -e mukeOnline.postman_environment.json -r junit --reporter-junit-export newman\xmlReport.xml

A19

The running result test report can be viewed in Test Result.

A20

Set the scheduled task
Build periodically to build H 10 * * * every day at 10 am, as shown in the figure below.

A21

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Strive hard and keep surpassing, every effort is a step towards success; love life, be positive, and feel the beauty of the world with your heart. Believe in your ability, bravely climb the peak, create your own magnificent chapter, and bloom the gorgeous flowers of life.

On the way of chasing dreams, time is our most precious capital, persistence is the driving force for achievement, and struggle is the code to create brilliance. Believe in yourself, not afraid of difficulties, and create a splendid life with sweat and wisdom.

Dreams are the light that guides the way forward, and struggle is the action to realize dreams; bravely break through the shackles, surpass self-limitation, and release the inner enthusiasm and talent. Believe in your own strength, persevere, and create a brilliant life in pursuit of excellence.

Guess you like

Origin blog.csdn.net/shuang_waiwai/article/details/132042873