Node.js單元測試、集成測試、基準測試以及代碼覆蓋率測試方面總結(2)

發表于:2016-05-17來源:推酷作者:OurJS點擊數: 標簽:
language: node_js node_js: - 6 - 5 before_script: script: - npm test - node benchmark/index.js after_script: 默認情況下,Travis CI 會自動安裝依賴并執行 npm test 命令,通過 script 字

  language: node_js

  node_js:

  - "6"

  - "5"

  before_script:

  script:

  - npm test

  - node benchmark/index.js

  after_script:

  默認情況下,Travis CI 會自動安裝依賴并執行 npm test 命令,通過 script 字段可以自定義需要執行的命令,其完整的生命周期包括:

  Install apt addons

  before_install

  install

  before_script

  script

  after_success or after_failure

  OPTIONAL before_deploy

  OPTIONAL deploy

  OPTIONAL after_deploy

  after_script

  基準測試

  基準測試使用嚴謹的測試方法、測試工具或測試系統評估目標模塊的性能,常用于觀測軟硬件環境發生變化后的性能表現,其結果具有可復現性。在 Node.js 環境中最常用的基準測試工具是 Benchmark.js ,安裝方式:

  npm install --save-dev benchmark

  基本示例:

  const Benchmark = require('benchmark');

  const suite = new Benchmark.Suite;

  suite.add('RegExp#test', function() {

  /o/.test('Hello World!');

  })

  .add('String#indexOf', function() {

  'Hello World!'.indexOf('o') > -1;

  })

  .on('cycle', function(event) {

  console.log(String(event.target));

  })

  .on('complete', function() {

  console.log('Fastest is ' + this.filter('fastest').map('name'));

  })

  // run async

  .run({ 'async': true });

  代碼覆蓋率

  代碼覆蓋率工具根據測試用例覆蓋的代碼行數和分支數來判斷模塊的完整性。AVA 推薦使用 nyc 測試代碼覆蓋率,安裝 nyc:

  npm install nyc --save-dev

  修改 .gitignore 忽略相關文件:

  node_modules

  coverage

  .nyc_output

  修改 package.json 中的 test 字段:

  {

  "scripts": {

  "test": "nyc ava"

  }

  }

  執行 npm test ,得到:

  ? test-in-action (master) ? npm test

  > test-in-action@1.0.0 test /Users/sean/Desktop/test-in-action

  > nyc ava

  2 passed

  ----------|----------|----------|----------|----------|----------------|

  File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |

  ----------|----------|----------|----------|----------|----------------|

  ----------|----------|----------|----------|----------|----------------|

  All files | 100 | 100 | 100 | 100 | |

  ----------|----------|----------|----------|----------|----------------|

  參考資料

原文轉自: http://ourjs.com/detail/5738493888feaf2d031d24fa

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97