Newman is postman's command-line tool, which executes Postman's script (collection) through the command line. Therefore, by executing scripts through Newman, continuous integration of postman interface testing can be realized on Jenkins, which is a very simple and convenient method for interface testing. It mainly includes the following steps:
- Prepare the software environment
- Prepare test script
- Newman Execution Script
- Create a Jenkins project
Environmental preparation
The process of tool installation and configuration will not be described in detail here, please check it online.
- Install Jenkins At present, there are a lot of information about the installation and configuration of Jenkins on the Internet. There are two relatively simple ways:
- Docker installation
- Run the war package directly
java -jar jenkins_located_path/jenkins.war --httpPort=88 &
Install newman Use the commandnpm install -g newman
newman to install the tool globally. The premise is that nodejs and npm have been installed. You can also install newman through the Taobao NPM mirrornpm install -g newman
, and use the commandnewman -v
to check whether the installation is successful.
Install the html report tool Use the commandnpm install -g newman-reporter-html
to install the html report template, which is convenient for viewing the test case execution results
test script
How to write Postman test scripts, there are a lot of information on the Internet, please check it yourself. Postman is always being updated, it is recommended to check the official website documentation .
Postman's test script consists of 3 parts:
- Test case script (collection) Newman executes test cases in units of collection, and there are two ways to obtain test scripts (collection):
- Export the Json format of the collection through postman, and view the official website documents ;
-
- Get the URL address of the script (collection), check the official document
- Environment variables (enviroment) Export corresponding environment variables in Postman's "MANAGE ENVIRONMENTS", in Json format.
- Global variables (global) Download global variables in Postman's "MANAGE ENVIRONMENTS", in Json format.
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036
Newman execution script (collection)
Use newman run -h
the command to view instructions for using the Newman execution script.
ubuntu001:~$ newman run -h
Usage: run <collection> [options]
URL or path to a Postman Collection.
Options:
-e, --environment <path> Specify a URL or Path to a Postman Environment.
-g, --globals <path> Specify a URL or Path to a file containing Postman Globals.
--folder <path> Specify folder to run from a collection. Can be specified multiple times to run multiple folders (default: )
-r, --reporters [reporters] Specify the reporters to use for this run. (default: cli)
-n, --iteration-count <n> Define the number of iterations to run.
-d, --iteration-data <path> Specify a data file to use for iterations (either json or csv).
--export-environment <path> Exports the environment to a file after completing the run.
--export-globals <path> Specify an output file to dump Globals before exiting.
--export-collection <path> Specify an output file to save the executed collection
--postman-api-key <apiKey> API Key used to load the resources from the Postman API.
--delay-request [n] Specify the extent of delay between requests (milliseconds) (default: 0)
--bail [modifiers] Specify whether or not to gracefully stop a collection run on encountering an errorand whether to end the run with an error based on the optional modifier.
-x , --suppress-exit-code Specify whether or not to override the default exit code for the current run.
--silent Prevents newman from showing output to CLI.
--disable-unicode Forces unicode compliant symbols to be replaced by their plain text equivalents
--global-var <value> Allows the specification of global variables via the command line, in a key=value format (default: )
--color <value> Enable/Disable colored output. (auto|on|off) (default: auto)
--timeout [n] Specify a timeout for collection run (in milliseconds) (default: 0)
--timeout-request [n] Specify a timeout for requests (in milliseconds). (default: 0)
--timeout-script [n] Specify a timeout for script (in milliseconds). (default: 0)
--ignore-redirects If present, Newman will not follow HTTP Redirects.
-k, --insecure Disables SSL validations.
--ssl-client-cert <path> Specify the path to the Client SSL certificate. Supports .cert and .pfx files.
--ssl-client-key <path> Specify the path to the Client SSL key (not needed for .pfx files)
--ssl-client-passphrase <path> Specify the Client SSL passphrase (optional, needed for passphrase protected keys).
-h, --help output usage information
Commonly used parameters are introduced below:
- Mandatory parameter
<collection>
, the path of the Json format file of the test script (collection), or the URL address - Optional parameter
-e, --environment <path>
, specify the path or URL address of the Json format file of the environment variable - Optional parameter
-g, --globals <path>
, specify the path or URL address of the Json format file of the global variable - Optional parameter
-r, --reporters [reporters]
, specify the file format of the test report, the default is cli (command line output), also supports html, Json format - Optional parameter
-n, --iteration-count <n>
, define the number of times the test script is executed, the default is 1
For example:
newman run APITestForAPP.postman_collection.json -e APP.dev_environment.json -g postman_globals.json -r cli,html --reporter-html-export report/reportAPP.html
The console outputs the command line report, and the html report reportAPP.html is generated under the execution path:
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 40 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 80 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 40 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 121 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 4.4s │
├───────────────────────────────────────────────┤
│ total data received: 11.31KB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 49ms │
└───────────────────────────────────────────────┘
The following command will output the same result.
newman run https://www.getpostman.com/collections/e260b7ccb7ee71eb761a -e APP.dev_environment.json -g postman_globals.json -r cli,html --reporter-html-export report/reportAPP.html
Create a Jenkins project
Create a new pipeline project (Pipeline). For the specific creation process, please refer to the script of Jenkins to create a pipeline project . In the project, the change of the interface involves 3 clients, the domains of the URL addresses of each end are different, and the interface implementation is the same. In an interface test, it is best to run the interfaces on all three ends. The Pipeline script of Jenkins is as follows:
node('229') {
stage('Preparation') {
git credentialsId: '6a9***8-6bf7-445e-aa78-94****4', url: 'http://gitlab.****.com/****/testcase_***_APITest.git'
}
stage('testWebAPI') {
catchError {
sh '''#!/bin/bash -il
newman run APITestForWeb.postman_collection.json -e Web.dev_environment.json -g postman_globals.json -r cli,html --reporter-html-export report/reportWeb.html
'''
}
if (currentBuild.result == 'FAILURE')
{
step([$class: 'Mailer', notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])])
}
}
stage('testAppAPI') {
catchError {
sh '''#!/bin/bash -il
newman run APITestForAPP.postman_collection.json -e APP.dev_environment.json -g postman_globals.json -r cli,html --reporter-html-export report/reportAPP.html
'''
}
if (currentBuild.result == 'FAILURE')
{
step([$class: 'Mailer', notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])])
}
}
stage('testCashierAPI') {
catchError {
sh '''#!/bin/bash -il
newman run APITestForCashier.postman_collection.json -e Cashier.dev_environment.json -g postman_globals.json -r cli,html --reporter-html-export report/reportCashier.html
'''
}
if (currentBuild.result == 'FAILURE')
{
step([$class: 'Mailer', notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])])
}
}
stage('Results') {
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, keepAll: true, reportDir: '/home/jenkins/work/workspace/APITest_Daily/report', reportFiles: 'reportWeb.html', reportName: 'HTML Report Web', reportTitles: 'Web端接口测试'])
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, keepAll: true, reportDir: '/home/jenkins/work/workspace/APITest_Daily/report', reportFiles: 'reportAPP.html', reportName: 'HTML Report APP', reportTitles: 'APP端接口测试'])
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, keepAll: true, reportDir: '/home/jenkins/work/workspace/APITest_Daily/report', reportFiles: 'reportCashier.html', reportName: 'HTML Report Cashier', reportTitles: '收银台端接口测试'])
}
}
The execution result is shown in the figure:
The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!
Software testing interview applet
The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!
The following interview question sections are covered:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics
Information acquisition method: