三.Newman安裝,配置和測試結果輸出


Newman
第一步:安裝Newman

  打開cmd命令窗口,執行:npm install -g newman

 安裝完成后,輸入newman -v,如下出現版本號表示安裝成功,如下圖:

第二步:.如果想要生成測試結果,還需要安裝測試結果的依賴包,newman支持多種形式的測試報告,但是一般html測試報告比較直觀,因此建議安裝html測試模板。

  (1) 官方默認的html包為:newman-reporter-html,但是該html頁面本人認為有些簡陋(其實就是不好看),如下圖:

  但是,還有另外一個html包為:newman-reporter-htmlextra(extra是額外的意思,可以理解為這是html模板的升級版),並且該報告還可以進行定制,可以自己修改一些東西改成自己想要的樣子(比如我把英文均改成了中文,主題開關打開,自定義報告名和字體大小等,方便查看),測試結果報告頁面如下圖:

想要了解關於該測試報告的更多信息,可以參考:https://github.com/DannyDainton/newman-reporter-htmlextra#newman-reporter-htmlextra

(2) 執行:npm install -g newman-reporter-htmlextra來下載該測試結果包

第三步:執行測試用例集,並生成測試報告(此方法有局限性,可以看下作為了解,具體生成測試報告方法建議采用第四步中方法)

  1.newman命令說明

  因此,如果使用cmd命令生成測試報告,即可以用: 

  執行:newman run 買賣房源接口.json -e 17環境.json -d D:\Test\test.csv --reporters htmlextra --reporter-htmlextra-export  D:\Test\test.html

  此時,就會在D:\Test\的文件夾下生成一個test.html的測試報告

第四步:采用執行JS腳本的方法生成測試報告(該方法便於管理一個項目的配置,文件等,並且便於后續持續集成),詳情可參考:https://github.com/DannyDainton/newman-reporter-htmlextra#newman-reporter-htmlextra

1.本地創建一個JS文件,輸入如下代碼:

!/usr/bin/env node

const newman = require('newman');
const csv = require('csvtojson');

//讀取csv文件使其轉換成json格式
const csvFilePath = "C:\Users\Administrator\Desktop\test\待售房源接口\SaleHouseCsv.csv";
var fs = require('fs');
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
jsonObj = JSON.stringify(jsonObj);
fs.writeFile("C:/Users/Administrator/Desktop/test/待售房源接口/newData.json",jsonObj,'utf-8', function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});

//麥脈-接口-買賣房源
newman.run({
collection: require('C:\Users\Administrator\Desktop\test\麥脈-接口-買賣房源\麥脈-接口-買賣房源.postman_collection.json'),
environment: require('C:\Users\Administrator\Desktop\test\麥脈-接口-買賣房源\麥脈17環境.postman_environment.json'),
iterationData: require('C:\Users\Administrator\Desktop\test\待售房源接口\newData.json'),
reporters: 'htmlextra',
reporter: {
htmlextra: {
export: 'C:\Users\Administrator\Desktop\test\待售房源接口\test_result.html',
darkTheme: false, // optional, tells the reporter to use the Dark Theme template
browserTitle:'測試部',
title: '接口自動化測試報告',
logs:true,
titleSize:4
}
}
}, function (err) {
if (err) { throw err; }
console.log('Collection complete,please go to the folder to view the test results');
});

2.可以看出,該段代碼相當於把cmd命令所執行的內容通過js代碼方便的管理起來,后續只需要維護這份js代碼即可。

3.所有的配置都在JS文件中了,此時只需要在cmd命令窗口執行該JS文件即可,

執行:node xxx.js(xxx.js是你的這段js代碼存放位置)

執行完成后,將會在你js中指定的文件夾中生成對應的測試報告,

至此,Newman的環境就配置好了。現在,你可以完全使用Postman + Node.js + Newman進行接口測試並生成可視的測試報告了,恭喜你!

該文章為原創,如有轉載請標明出處!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM