Postman 使用Newman定制reporters

2023-04-06 10:47 更新

自定義報(bào)告器可用于使用 Newman 生成滿足特定用例的收集運(yùn)行報(bào)告,例如,在請(qǐng)求(或其測(cè)試)失敗時(shí)注銷響應(yīng)主體。

建立自定義記者

自定義報(bào)告器是一個(gè)名稱為 的 Node.js 模塊newman-reporter-<name>。

要?jiǎng)?chuàng)建自定義報(bào)告程序,請(qǐng)執(zhí)行以下操作:

  1. 在您選擇的目錄中,創(chuàng)建一個(gè)帶有npm init.
  2. 添加一個(gè)index.js文件,該文件導(dǎo)出以下形式的函數(shù):function (emitter, reporterOptions, collectionRunOptions) { // emitter is is an event emitter that triggers the following events: https://github.com/postmanlabs/newman#newmanrunevents // reporterOptions is an object of the reporter specific options. The usage examples below have more details. // collectionRunOptions is an object of all the collection run options: // https://github.com/postmanlabs/newman#newmanrunoptions-object--callback-function--run-eventemitter };
  3. 使用 發(fā)布您的記者npm publish,或在本地使用您的記者。閱讀使用說明以獲取更多信息。

@myorg/newman-reporter-<name>還支持Scoped reporter 包名稱。

使用自定義記者

要使用自定義報(bào)告程序,必須先安裝它。例如,要使用 Newman TeamCity 報(bào)告程序,請(qǐng)安裝報(bào)告程序包:

npm install newman-reporter-teamcity

請(qǐng)注意,包裹名稱的格式為newman-reporter-<name>,其中<name>是報(bào)告者的實(shí)際姓名。如果全局安裝了 Newman,則安裝是全局的,否則是本地的。npm install ...使用-g全局安裝標(biāo)志運(yùn)行。

要使用本地(未發(fā)布的)記者,請(qǐng)改為運(yùn)行命令npm install <path/to/local-reporter-directory>。

通過命令行工具或以編程方式使用已安裝的報(bào)告程序。此處,newman-reporter在選項(xiàng)中指定報(bào)告者名稱時(shí)不需要前綴。

范圍報(bào)告程序包必須使用范圍前綴指定。例如,如果您的包名稱是@myorg/newman-reporter-name,則必須使用 指定報(bào)告者@myorg/name。

在命令行上:

newman run /path/to/collection.json -r myreporter --reporter-myreporter-<option-name> <option-value> # The option is optional

以編程方式:

var newman = require('newman');

newman.run({
   collection: '/path/to/collection.json',
   reporters: 'myreporter',
   reporter: {
     myreporter: {
       'option-name': 'option-value' // this is optional
     }
   }
}, function (err, summary) {
  if (err) { throw err; }
  console.info('collection run complete!');
});

在上述兩種情況下,記者選項(xiàng)都是可選的。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)