postman一直在loading+Newman+Jenkins构建一直失败是什么情况

Postman+Newman+Jenkins APItest自动化集成测试
时间: 17:11:41
&&&& 阅读:228
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&&& postman做接口测试简单易用很容易上手,但是接口越来越多,每次手动点击runner进行测试不灵活,最近就研究了下newman,利用newman实现接口的自动化测试,但是每次需要命令行操作也不方便,就想着利用Jenkins做真正的自动化测试,刚好部门有现成的Jenkins环境就用了起来,目前已经实现了接口的自动化测试。postman导出的json文件直接上传git,Jenkinsjob配置git路径,每次只要点击下job就可以进行接口测试或者有新的接口更新也会实现自动化测试,同时可以发送邮件告知同事测试结果,非常方便。
一、环境配置
1、安装postman:Google插件下载或者postman应用下载,很简单,不说了
2、安装newman:
第一步,安装nodejs。很简单,之前的appium使用中有讲过,感兴趣的可以自己去看,这里不再赘述。&第二步,在nodejs命令行安装newman,即命令行输入如下命令:
npm &install -g newman
3、安装Jenkins:下载war包或者zip包安装都可以,具体在之前的sonarqube持续化有讲过,这里不再赘述。
环境配置完成就可以进行json文件的导出了。
二、postman接口文件导出
为了满足能够同时运行所有的API及只运行指定的API集合,在postman中创建一个collection,里面包含多个folder,具体好处后面便知。
点击collection右侧的。。。按钮,选择Export,弹框中选择Export按钮,就可以把collection中的内容导出到本地,存为json格式的。
这里为了切换环境方便及变量使用方便,我配置了环境变量及全局变量,为了能在newman中运行起来,环境变量及全局变量也要导出,具体如下:
点击postman右侧的设置按钮,选择manage Environments,点击环境变量后面的下载按钮,即可将环境变量导出,导出的格式为json类型。
同样,点击manage Environments页面中的Global,点击Download as JSON按钮,即可导出全局变量
相关的文件数据导出后就可以在newman中进行验证性测试了。
三、Newman的使用
1、本地运行自动化测试命令:
newman -c C:\Users\hexueqin\Desktop\serverApiTest.postman_collection.json----这种写法已经废弃,但是用还是可以用的,不过还是用最新的好,否则Jenkins上会报错。
2、生成测试报告,支持html,xml,json,cli4种。
newman run C:\Users\hexueqin\Desktop\serverApiTest.postman_collection.json& --reporters html,junit --reporter-html-export d:\html01.html --reporter-junit-export&
3、含有环境变量,全局变量的自动化测试命令:指定执行对应的folder
常见命令:
run --运行newman的命令
-e &加载环境变量的数据
-g &加载全局变量的数据
-n case运行次数,默认运行次数为1次
--folder &运行collection中的指定folder
--reporters html,junit &指定生成报告的类型
其它命令可在github上查看:
newman run C:\Users\hexueqin\Desktop\apiTest.postman_collection.json -e C:\Users\hexueqin\Desktop\dev-environment.postman_environment.json -g C:\Users\hexueqin\Desktop\globals.postman_globals.json --folder visitApi --reporters html --reporter-html-export d:\result.html
本地运行结果显示:
注意:如果指定生成报告,就不显示具体的请求执行及结果。
本地生成HTML文件的查看:
四、Jenkins中job配置及运行
1、新建job,名字自己取即可
2、配置脚本:(需要在Jenkins服务器本地安装newman程序)
3、添加HTML报告:
4、运行job后查看报告:
自此,postman自动化测试可以顺利的跑起来了,邮件暂时没有添加,具体添加可以参考sonarqube中的配置。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/sunshine-sky66/p/7122347.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!接口自动化测试--postman与Jenkins持续集成 - CSDN博客
接口自动化测试--postman与Jenkins持续集成
postman是谷歌的一款非常好用的工具,可以用来做手工的接口测试,也能实现简单的接口自动化持续集成,如果大家觉得postman已经能满足自己的需要,也不想去学习Python+requests,那么postman这个工具一定要好好掌握。
nodejs+newman
安装nodejs
nodejs官网下载地址: 选择适合自己系统的傻瓜式安装
安装完成后,配置一下环境变量
cmd中输入指令: node -v 查看node的版本,出现node版本说明我们安装成功,环境变量也配置ok
安装newman
命令行输入指令 npm install newman –global
安装完成后,输入指令 newman -v 查看版本,检测是否安装成功,这里注意,newman的包被安装在了npm工具的包下
出现了newman的版本,说明我们安装ok
postman导出collection
postman界面的左侧边栏,选择collections,找到自己需要导出的集合,选择更多按键,点击Export
点击export之后,弹出的窗口,我们选择v2,并导出到某一个我们选择的文件夹下
导出的collection是json文件
命令行运行collection
cmd进入到collection的json文件的目录下,输入指令
$ newman run postman入门.postman_collection.json
可以看到上图中我们3个断言全部通过
Jenkins创建项目
其实到这里基本上玩过Jenkins的应该都明白了,我们接下来需要在Jenkins新建一个项目,按照流程配置一下(这里不细讲)
看一下执行构建的windows命令,给大家一点tip
这里的”cd\”是因为博主本身的Jenkins默认的工作环境就是G盘的Apache目录,所以需要切换回根目录
newman指令非内部或外部指令
如果在测试构建的过程中碰到以下问题
这是因为Jenkins的工作环境下与windows的默认环境有所区别,我们只需要配置一下Jenkins的环境变量即可
系统管理-&管理节点-&选择需要使用的主机-&配置从节点
新建一个键值对列表,键名PATH,把npm和nodejs的路径放进来即可
点击保存之后我们再去构建就没有问题了。
newman生成测试报告
newman运行可以生成各类测试报告,有html,cli,json格式等
指令:newman run xxx.postman_collection.json -r html[,cli,json,junit]
如果要生成多个格式,用逗号隔开不要留空格
当然支持给测试报告命名和指定生成路径
指令:newman run xxx.postman_collection.json -r html –reporter-html-export G:\name.html
可以定义测试报告的生成位置以及它的名称
本文已收录于以下专栏:
相关文章推荐
1 背景本文要介绍的环境在我司已经投入使用,举个简单的真实使用场景,开发提供了300多个API,每个API都有各种参数,所以我们会先在postman中为这300多个API编写300*n个testcas...
(这个是内部分享文档,侧重于测试报告的生成以及持续集成,至于接口框架的内部组织可以按照自己的想法来实现,有问题欢迎留言)1.环境:
操作系统:windows 64位
软件环境:py...
postman+jenkins+newman做接口测试的持续集成
为何要做接口自动化测试的持续集成?
1. 接口相对稳定,改动少,比起GUI自动化测试来说性价比更加高些,不容易出现GUI自动...
为什么要在Linux上构建自动化环境呢?
因为在Linux上执行速度要比在Windows快很多,以打包apk为例,至少快一倍。。。
如果公司每个版本要打几百个apk渠道包,这效率将大幅提升。
与时俱进,见此文:http://blog.csdn.net/hunterno4/article/details/
为什么要在Linux上构建自动化环境呢?
因为在Linux...
TMTS是淘宝的自动化测试构架,优缺点都较为明显
优点:最主要的就是已经实现出错截屏并提供日志
缺点:比较小众化,遇到问题也无人解答
自动化测试终究是要能够持续集成才能有更大的意义的,利用ro...
有人在我前面的帖子里面回复,可以用 Jmeter 来做接口测试. 稍微尝试了下,来给大家分享,完全是现学现卖,还请大家多多提意见。
Jmeter 这工具好用,又开源,我是看了一遍官方文档,将其捡起来...
在《selenium+testng+gitblit+jenkins+ant自动化测试系列七:jenkins安装及基础配置》的jenkins配置基础上继续操作。
测试项目名称仍为helloworld,...
他的最新文章
讲师:宋宝华
讲师:何宇健
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Postman-CI集成Jenkins
时间: 23:46:23
&&&& 阅读:158
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
官方说明:Postman‘s command-line companion lets you do amazing things! With Newman, you can integrate Postman collections with your build system. Or you can run automated tests for your API through a cron job.
概述:集成Postman的API测试命令行执行工具
基于Node.js,可安装在Linux/Windows/Mac
安装命令:
npm install -g newman
验证安装是否成功
出现版本号,则安装成功
运行方式,执行Postman导出的Collection文件
newman -c *.json.postman_collection
Newman命令行操作
执行指定的Collection文件
newman -c *.json.postman_collection
执行指点定的Collection文件及Environment文件
newman -c *.json.postman_collection -e *.postman_environment
执行保存到服务器上的Collection
newman -u /collections/cb208e7ee5
循环执行Collection文件5次
newman -u /collections/cb208e7ee5 -n 5
详细的newman命令用法
Usage: newman [options]
-h, --help
output usage information
-V, --version
output the version number
-c, --collection [file]
Specify a Postman collection as a JSON [file]
-u, --url [url]
Specify a Postman collection as a [url]
-f, --folder [folder-name]
Run a single folder from a collection. To be used with -c or -u
-e, --environment [file]
Specify a Postman environment as a JSON [file]
-E, --exportEnvironment [file]
Specify an output file to dump the Postman environment before exiting [file]
-d, --data [file]
Specify a data file to use either json or csv
-g, --global [file]
Specify a Postman globals file [file]
-G, --exportGlobals [file]
Specify an output file to dump Globals before exiting [file]
-y, --delay [number]
Specify a delay (in ms) between requests
-r, --requestTimeout [number]
Specify a request timeout (in ms) for requests
-R, --avoidRedirects
Prevents Newman from automatically following redirects
-s, --stopOnError
Stops the runner with code=1 when a test case fails
-j, --noSummary
Doesn‘t show the summary for each iteration
-n, --number [number]
Define the number of iterations to run
-C, --noColor
Disable colored output
-k, --insecure
Disable strict ssl
-N, --encoding [encoding-type]
Specify an encoding for the response. Supported values are ascii,utf8,utf16le,ucs2,base64,binary,hex
-x, --exitCode
Continue running tests even after a failure, but exit with code=1. Incompatible with --stopOnError
-o, --outputFile [file]
Path to file where output should be written [file]
-O, --outputFileVerbose [file]
Path to file where full request and responses should be logged [file]
-t, --testReportFile [file]
Path to file where results should be written as JUnit XML [file]
-i, --import [file]
Import a Postman backup file, and save collections, environments, and globals [file] (Incompatible with any option except pretty)
-p, --pretty
Enable pretty-print while saving imported collections, environments, and globals
-H, --html [file]
Export a HTML report to a specified file [file]
-W, --whiteScreen
Black text for white screen
-L, --recurseLimit [limit]
Do not run recursive resolution more than [limit] times. Default = 10. Using 0 will prevent any varialbe resolution
Newman is a command-line collection runner for Postman. You must specify a collection file or a collection URL to run newman
A collection file or URL is mandatory
newman -c POSTMAN_COLLECTION
newman -u POSTMAN_COLLECTION -e POSTMAN_ENVIRONMENT
newman -c POSTMAN_COLLECTION -f SAMPLE_FOLDER
For more documentation, issues and examples head over to /postmanlabs/newman
Jenkins集成
既然Newman支持命令行运行,那么集成Jenkins就很简单。在Jenkins的Job中添加command line,内容如下:
newman -c *.json.postman_collection --exitCode 1
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/s/p/5037591.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生![文章][乐呵]
接口自动化之Postman+Newman
时间: &&&&
阅读:78 &&&&
评论: &&&&
收藏:0 &&&&
原文:/testdream/p/6192866.html
  Postman 使一款可以方便我们调用API的工具,通过Postman 与 Newman结合我们还可以批量运行API达到API自动化测试的目的。
Postman 安装
  Window 系统需要先安装Chrome浏览器,然后在应用商店找到Postman插件,直接点击安装便可:
测试GET类型API以豆瓣搜索图书API为例
这个接口的method为get,postman默认为get,在url栏输入被测接口
在接口文档中,q与tag其中的一个是必填项,所以这里需要配置参数,点击url右侧的params,并输入参数
点击send按钮发送请求,在下面可以看到返回的response,响应的状态码,与响应时间
response还可以以不同的方式查看,并且还可以看到cookies,headers信息
每次以不同的参数或者设置,在左侧history都会帮我们记录一个接口测试历史,以便于下次重新调用
测试POST类型API
以Postman自带的接口为例选取请求的方法为POST,并输入接口地址,因为post请求大多是json形式,所以可以打开Body选项,并选择JSON(application/json)形式,输入请求的json,同样其余的操作跟GET接口一样。
变量的设置
编写的API往往需要在多个环境下执行,而Postman 提供了两种类型的变量:环境变量和全局变量,从而很好的解决了这个问题。环境变量有效范围仅仅在于你所选取的环境,全局变量对所有的环境都试用。
api可能需要在拨通的环境中运行,所以api请求的服务器地址不能写死,希望是可以配置的,创建环境变量有多种方式。
手工预先创建环境变量
代码自动创建环境变量自动新建环境变量可在两种情况下创建,但是创建方式相同都是用了postman提供的方法:postman.setEnvironmentVariable("key", "value");
在某个请求发起之前创建:在Pre-request Script标签里面添加代码:
在某个请求发起之后创建:在Tests标签里面添加如下
全部变量跟环境变量的创建类似,也可以通过手工预先创建或者通过代码去创建。
通过手工预先创建
通过代码创建2.1 在请求发起前创建在Pre-request Script标签里面添加代码:
postman.setGlobalVariable("key", "value");
2.2 在请求发起后创建在Tests标签里面添加如下:
postman.setGlobalVariable("key", "value");
变量的引用
在需要的地方加上{{变量名}}便可
PostMan 除了提供环境变量和全局变量外,还提供了三种随机数。{{$guid}}:添加一个V4风格GUID
{{$timestamp}}:将当前的时间戳,精确到秒
{{$randomInt}}:添加0和1000之间的随机整数
  Postman可以在tests里面,用自带的脚本对接口进行测试,单击tests标签后可以看到右侧有个snippets栏,里面就是postman内置的测试脚本,辅助对接口进行测试。
  选择其中的一个脚本,修改其中的参数,再次点击send,这时response的tests出现了一个1/1,说明执行一个测试并且通过测试,绿色pass说明验证通过
  红色fail说明测试不通过,一个接口可以写多个脚本进行多次测试
  内置脚本说明:
1. 清除一个全局变量
& & Clear a global variable
& &对应脚本:
& &postman.clearGlobalVariable("variable_key");
& &参数:需要清除的变量的key2.清除一个环境变量
& &Clear an environment variable
& &对应脚本:
& &postman.clearEnvironmentVariable("variable_key");
& &参数:需要清除的环境变量的key3.response包含内容
& &Response body:Contains string
& &对应脚本:
& &tests["Body matches string"] =responseBody.has("string_you_want_to_search");
& &参数:预期内容4.将xml格式的response转换成son格式
& &Response body:Convert XML body to a JSON Object
& &对应脚本: & &var jsonObject = xml2Json(responseBody);
& &参数:(默认不需要设置参数,为接口的response)需要转换的xml5.response等于预期内容
& &Response body:Is equal to a string
& &对应脚本:
& &tests["Body is correct"] = responseBody === "response_body_string";
& &参数:预期response6.json解析key的值进行校验
& &Response body:JSON value check
& &对应脚本:
& &tests["Args key contains argument passed as url parameter"] = ‘test‘ in responseJSON.args
& &参数:test替换被测的值,args替换被测的key7.检查response的header信息是否有被测字段
& &Response headers:Content-Type header check
& &对应脚本:
& &tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
& &参数:预期header8.响应时间判断
& &Response time is less than 200ms
& &对应脚本:
& &tests["Response time is less than 200ms"] = responseTime & 200;
& &参数:响应时间9.设置全局变量 & & &Set an global variable
& & &对应脚本:
& & &postman.setGlobalVariable("variable_key", "variable_value");
& & &参数:全局变量的键值10.设置环境变量 & & &Set an environment variable
& & &对应脚本:
& & &postman.setEnvironmentVariable("variable_key", "variable_value");
& & &参数:环境变量的键值11.判断状态码
& & &Status code:Code is 200
& & &对应脚本:
& & &tests["Status code is 200"] = responseCode.code != 400;
& & &参数:状态码12.检查code name 是否包含内容
& & &Status code:Code name has string
& & &对应脚本:
& & &tests["Status code name has string"] = responseCode.name.has("Created");
& & &参数:预期code name包含字符串13.成功的post请求
& & &Status code:Successful POST request
& & &对应脚本:
& & &tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;14.微小验证器
& & & Use Tiny Validator for JSON data & & & & & &
& & & 对应脚本:
& & & &var schema = { & & & & "items": { & & & & "type": "boolean"
& & & & & & }
& & & & }; & & & &var data1 = [true, false]; & & & &var data2 = [true, 123]; & & & &console.log(tv4.error);
& & & &tests["Valid Data1"] = tv4.validate(data1, schema);
& & & &tests["Valid Data2"] = tv4.validate(data2, schema);
& & & &参数:可以修改items里面的键值对来对应验证json的参数
使用Runner功能和外部数据
  Postman 工具自带了Runner功能,用于批量运行脚本。在运行时还可以使用外部的CSV或者json文件来指定数据。  例如现在新建了如下两个外部数据,第一个保存为.json文件,第二个保存为.csv文件。
  新建如下GET请求API,并放于单独一个文件夹中管理。接口请求中{{host}}便是用来获取上步新建的两个文件夹中的数据,{{}}中的名字对应json文件的key值,对应csv文件中的第一行值。
  点击Runner按钮,打开Runner界面:
  在新打开的窗口中,选着你要刚新建的文件夹名,选择你要运行的环境,运行的次数和在Data File中选择刚新建的外部json或者csv文件,并选取文件类型,点击Start Test,变开始逐条读取外部文件中的数据,进行运行。
Postman Interceptor
Interceptor 可以直接从浏览器中获取请求,并保存在Postman的History中。 这个插件可以大大缩短API配置的时间,同样Interceptor还有一个功能可以让Postman和Chrome浏览器共用Chrome的Cookies。
安装 Interceptor
  Interceptor 同样是Chrome的一个插件,所以也可以从Chrome网上商店找到该插件:
,直接点击安装。安装完后会在Chrome 插件中找到下图标识。
使用 Interceptor
  开启Interceptor插件,并设置你要抓取的网站请求
  在Postman 上同样打开Interceptor
  这时在Chrome浏览器上访问一些baidu相关域名就会自动被Postman抓取,并在Postman的History显示
官方帮助文档地址:
Newman 安装
  嗯,它需要安装,因为它不是音乐播放器!Newman是为Postman而生,专门用来运行Postman编写好的脚本。Newman安装步骤:
需要安装nodejs,并配置好环境
打开控制台,运行:npm install -g newman
Paste_Image.png
校验是否安装成功,运行:newman --version
Newman 执行脚本
  Newman在3版本后做了比较大的改动,但是运行命令越来越简单如下:
newman run
run 后面跟上要执行的json文件或者URL(json 和 URL 都由postman导出生成),再后面跟一些参数,例如环境变量,测试报告,接口请求超时时间等等。最后给两个完整的例子做参考:例子1,通过newman 运行postman导出的test1.json文件,并生成多种测试报告(json,junit的xml,html):
newman run c:\test1.json --reporters cli,html,json,junit --reporter-json-export jsonOut.json --reporter-junit-export xmlOut.xml --reporter-html-export htmlOut.html
例子2,运行 )中的所有api,并使用env.json作为环境变量和globals.json作为全局变量,并使用外部data.csv作为外部数据,最后设置了接口请求超时时间为5S 。
newman run https:
Jenkins 结合
  平时做接口自动化,避免不了最后通过Jenkins做构建。既然Newman提供了控制台命令执行方式,那么像通过Jenkins来构建也就容易多了。步骤一:在Jenkins 机器上安装Newman步骤二:搭建Jenkins环境,并新建个自由风格的Job步骤三:构建选择Execute Windows batch command,并输入newman 运行命令
步骤四:因为上面命令中构建会生成junit的xml报告,所以可以在构建后用Publish JUnit test result report 插件来生成测试报告。
&&&&&&&&长按二维码识别关注,您的支持是我们最大的动力。 && &&&&
&&&&&&&&公众号:测试梦工厂
&&&&&&&&QQ一群:
?标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
版权所有 鲁ICP备号-6Jenkins+postman+newman
时间: 13:13:43
&&&& 阅读:75
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
下载postman安装
  这个在其他文章中已经说过了,不要安装插件安装APP即可
newman 的安装
1、安装nodejs。&2、在nodejs命令行安装newman,即命令行输入如下命令
<span style="color: # npm install -g newman2 Newman -h
//验证是否安装成功
jenkins 安装
  官网下载,下一步下一步即可安装
命令行运行接口
在postman中设置请求,并保存testcase
设置好环境变量并导出文件
在postman下载collection
在命令行执行脚本,如下: newman run &collection-file-source& [options]
jenkins 配置postman
在jenkins上配置如下图,这个路径就是上面通过postman导出文件的路径。&
最后生成的报告路径如下:
剩下的就是jenkins的常规操作了,譬如设置好邮箱后点击立即构建或者设置多久构建一次,这样自动化就跑起来了,等待自动化测试结束后我们就可以收到测试成功或者失败的测试报告邮件(依赖于你的设置)了。
通过上面这些步骤即可完成基于postman和Jenkins的自动化接口测试。
&标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/bell1991/p/7440129.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!}

我要回帖

更多关于 postman 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信