https://www.npmjs.com/package/newman

配置环境

安装node.js 环境

从node.js官网下载安装包 https://nodejs.org/en/download/
image.png

下载完成之后进行安装
image.pngimage.pngimage.pngimage.png

检验安装是否成功

命令行中运行

  1. npm -v

image.png
如果有对应的版本信息显示,说明安装成功。

安装newman

在命令行中执行命令

  1. npm install -g newman

image.png
执行上面命令 会自动从服务器上下载并安装newman命令, 安装成功之后会有对应的提示。

Newman使用

基本使用

  1. newman run collection的文件路径

单接口测试.postman_collection.json
createtopics.csv

运行

  1. newman run /Users/zack/Desktop/testdata/单接口测试.postman_collection.json -d /Users/zack/Desktop/testdata/createtopics.csv
  • run 执行postman中导出文件
  • -d 跟csv文件的路径
  • -g 后跟globals 全局环境变量文件路径。

执行结果

  1. zack@zackdeMacBook-Pro ~ % newman run /Users/zack/Desktop/testdata/单接口测试.postman_collection.json -d /Users/zack/Desktop/testdata/createtopics.csv
  2. newman
  3. 单接口测试
  4. Iteration 1/8
  5. → 新建主题
  6. POST http://47.100.175.62:3000/api/v1/topics [401 Unauthorized, 408B, 364ms]
  7. ✓ 状态码断言401
  8. ✓ 服务器返回结果中包含字段错误的accessToken
  9. Iteration 2/8
  10. → 新建主题
  11. POST http://47.100.175.62:3000/api/v1/topics [401 Unauthorized, 408B, 24ms]
  12. ✓ 状态码断言401
  13. ✓ 服务器返回结果中包含字段错误的accessToken
  14. Iteration 3/8
  15. → 新建主题
  16. POST http://47.100.175.62:3000/api/v1/topics [401 Unauthorized, 408B, 14ms]
  17. ✓ 状态码断言401
  18. ✓ 服务器返回结果中包含字段错误的accessToken
  19. Iteration 4/8
  20. → 新建主题
  21. POST http://47.100.175.62:3000/api/v1/topics [400 Bad Request, 462B, 20ms]
  22. ✓ 状态码断言400
  23. ✓ 服务器返回结果中包含字段标题不能为空
  24. Iteration 5/8
  25. → 新建主题
  26. POST http://47.100.175.62:3000/api/v1/topics [400 Bad Request, 471B, 15ms]
  27. ✓ 状态码断言400
  28. ✓ 服务器返回结果中包含字段标题字数太多或太少
  29. Iteration 6/8
  30. → 新建主题
  31. POST http://47.100.175.62:3000/api/v1/topics [200 OK, 457B, 27ms]
  32. 1. 状态码断言400
  33. 2. 服务器返回结果中包含字段标题字数太多或太少
  34. Iteration 7/8
  35. → 新建主题
  36. POST http://47.100.175.62:3000/api/v1/topics [400 Bad Request, 468B, 18ms]
  37. ✓ 状态码断言400
  38. ✓ 服务器返回结果中包含字段必须选择一个版块
  39. Iteration 8/8
  40. → 新建主题
  41. POST http://47.100.175.62:3000/api/v1/topics [400 Bad Request, 462B, 22ms]
  42. ✓ 状态码断言400
  43. ✓ 服务器返回结果中包含字段内容不可为空
  44. ┌─────────────────────────┬────────────────────┬───────────────────┐
  45. │ │ executed │ failed │
  46. ├─────────────────────────┼────────────────────┼───────────────────┤
  47. │ iterations │ 8 │ 0 │
  48. ├─────────────────────────┼────────────────────┼───────────────────┤
  49. │ requests │ 8 │ 0 │
  50. ├─────────────────────────┼────────────────────┼───────────────────┤
  51. │ test-scripts │ 8 │ 0 │
  52. ├─────────────────────────┼────────────────────┼───────────────────┤
  53. │ prerequest-scripts │ 0 │ 0 │
  54. ├─────────────────────────┼────────────────────┼───────────────────┤
  55. │ assertions │ 16 │ 2 │
  56. ├─────────────────────────┴────────────────────┴───────────────────┤
  57. │ total run duration: 676ms │
  58. ├──────────────────────────────────────────────────────────────────┤
  59. │ total data received: 425B (approx) │
  60. ├──────────────────────────────────────────────────────────────────┤
  61. │ average response time: 63ms [min: 14ms, max: 364ms, s.d.: 113ms] │
  62. └──────────────────────────────────────────────────────────────────┘
  63. # failure detail
  64. 1. AssertionErr… 状态码断言400
  65. iteration: 6 expected response to have status code 400 but got 200
  66. at assertion:0 in test-script
  67. inside "新建主题"
  68. 2. AssertionErr… 服务器返回结果中包含字段标题字数太多或太少
  69. iteration: 6 expected
  70. '{"success":true,"topic_id":"61557899f7759804f43d6afd…
  71. to include '标题字数太多或太少'
  72. at assertion:1 in test-script
  73. inside "新建主题"

使用命令行方式运行脚本,可以部署在服务器上通过设置定时任务 进行自动化执行接口测试。

在公司中,做自动化测试,一般都设置放在服务器端运行。

作业

假期作业:
将cnode 上面的所有接口都做一下 正常场景
要求:

  1. 使用上下游传参
  2. 每个接口都需要添加断言
  3. 使用newman命令能够运行

image.png

cnode-api.postman_collection.json
postman_globals.json

  1. zack@zackdeMacBook-Pro tmp % newman run cnode-api.postman_collection.json -g postman_globals.json
  2. newman
  3. cnode-api
  4. → 新建主题
  5. POST http://47.100.175.62:3000/api/v1/topics [200 OK, 457B, 692ms]
  6. ✓ Status code is 200
  7. ✓ 返回结果中包含字段
  8. → 主题详情
  9. GET http://47.100.175.62:3000/api/v1/topic/615d17fbf7759804f43d7faf [200 OK, 863B, 42ms]
  10. ✓ Status code is 200
  11. ✓ 返回结果中应该包含615d17fbf7759804f43d7faf
  12. → 收藏主题
  13. POST http://47.100.175.62:3000/api/v1/topic_collect/collect [200 OK, 362B, 17ms]
  14. ✓ 状态码为200
  15. ✓ Body matches string
  16. → 编辑主题
  17. POST http://47.100.175.62:3000/api/v1/topics/update [200 OK, 400B, 17ms]
  18. ✓ 服务器返回的结果中一定包含话题id615d17fbf7759804f43d7faf
  19. → 取消主题
  20. POST http://47.100.175.62:3000/api/v1/topic_collect/de_collect [200 OK, 362B, 20ms]
  21. ✓ 状态码为200
  22. ✓ Body matches string
  23. → 新建评论
  24. POST http://47.100.175.62:3000/api/v1/topic/615d17fbf7759804f43d7faf/replies[200 OK, 457B, 24ms]
  25. ✓ Status code is 200
  26. → 点赞评论
  27. POST http://47.100.175.62:3000/api/v1/reply/615d17fbf7759804f43d7fb1/ups [200 OK, 376B, 18ms]
  28. ✓ Status code is 200
  29. ✓ 返回结果中包含字段
  30. → 用户详情
  31. GET http://47.100.175.62:3000/api/v1/user/fanmao-feixiaomin [200 OK, 4.35KB, 734ms]
  32. → 验证用户正确性
  33. POST http://47.100.175.62:3000/api/v1/accesstoken [200 OK, 468B, 14ms]
  34. → 获取未读消息
  35. GET http://47.100.175.62:3000/api/v1/message/count?accesstoken=057997b2-5a3c-408c-a57e-04853323eb39 [200 OK, 347B, 25ms]
  36. → 获取已读和未读消息
  37. GET http://47.100.175.62:3000/api/v1/messages?accesstoken=057997b2-5a3c-408c-a57e-04853323eb39 [200 OK, 7.36KB, 88ms]
  38. → 标记单个消息为已读
  39. POST http://47.100.175.62:3000/api/v1/message/mark_one/615d17fbf7759804f43d7fb2 [200 OK, 405B, 14ms]
  40. ✓ Status code is 200
  41. ✓ 服务器返回结果包含615d17fbf7759804f43d7fb2
  42. → 标记为全部已读
  43. POST http://47.100.175.62:3000/api/v1/message/mark_all [200 OK, 380B, 845ms]
  44. ┌─────────────────────────┬────────────────────┬────────────────────┐
  45. │ │ executed │ failed │
  46. ├─────────────────────────┼────────────────────┼────────────────────┤
  47. │ iterations │ 1 │ 0 │
  48. ├─────────────────────────┼────────────────────┼────────────────────┤
  49. │ requests │ 13 │ 0 │
  50. ├─────────────────────────┼────────────────────┼────────────────────┤
  51. │ test-scripts │ 9 │ 0 │
  52. ├─────────────────────────┼────────────────────┼────────────────────┤
  53. │ prerequest-scripts │ 0 │ 0 │
  54. ├─────────────────────────┼────────────────────┼────────────────────┤
  55. │ assertions │ 14 │ 0 │
  56. ├─────────────────────────┴────────────────────┴────────────────────┤
  57. │ total run duration: 2.7s │
  58. ├───────────────────────────────────────────────────────────────────┤
  59. │ total data received: 11.99KB (approx) │
  60. ├───────────────────────────────────────────────────────────────────┤
  61. │ average response time: 196ms [min: 14ms, max: 845ms, s.d.: 309ms] │
  62. └───────────────────────────────────────────────────────────────────┘