LoginSignup
2
1

More than 5 years have passed since last update.

TypeScript を使って Alexa Custom Skills を作ろう 番外編 カバレッジ

Posted at

はじめに

前回投稿した機能テスト編では、alexa-conversationを利用した機能テストについてご紹介しました。

本投稿では、単体テスト機能テスト編で作成したテストによって、スキルの実装コードがどれくらい網羅されているかを計測する為の方法をご紹介します。

カバレッジ計測ライブラリについて

今回は、情報量が多そうなistanbulを選択しました。

istanbul

カバレッジを見てみよう

ライブラリのインストール

移動
$ cd ~/custom-skill-sample-to-convert/skill/lambda/custom
istanbulのインストール
$ npm install --save-dev istanbul

package.jsonの修正

テストを実行する為のスクリプトをpackage.jsonに定義します。

  • tscコマンドでトランスパイル
  • istanbulでカバレッジ計測
  • mocha./dist/testsディレクトリ配下のテストファイルを再帰的にテスト
package.json
"scripts": {
  "test": "tsc && istanbul cover --print both $(npm bin)/_mocha --recursive $(find ./dist/tests -name '*.test.js')"
},

テスト実行

テスト実行
$ npm test
結果
Warning: Application ID is not set
  first-utteranceクラスのテスト
    メッセージが正しく取得できること
      ✓ インスタンスが作成されていること
      ✓ 想定した発話が返されること

  Executing conversation: タイろう テスト
    ✓ Finished executing conversation

  Conversation: タイろう テスト
    User triggers: AMAZON.HelpIntent
      ✓ Alexa's plain text response should equal: 声を聞かせて、と言ってください。


  4 passing (53ms)

=============================================================================
Writing coverage object [~/custom-skill-sample-to-convert/skill/lambda/custom/coverage/coverage.json]
Writing coverage reports at [~/custom-skill-sample-to-convert/skill/lambda/custom/coverage]
=============================================================================
-------------------------|----------|----------|----------|----------|----------------|
File                     |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
-------------------------|----------|----------|----------|----------|----------------|
 dist/                   |    92.31 |       50 |      100 |    92.31 |                |
  index.js               |    92.31 |       50 |      100 |    92.31 |             11 |
 dist/enums/             |      100 |      100 |      100 |      100 |                |
  handler-state-types.js |      100 |      100 |      100 |      100 |                |
 dist/handlers/          |    40.82 |    33.33 |     5.26 |    48.78 |                |
  first-handler.js       |    33.33 |    33.33 |        0 |    45.45 |... 29,34,37,40 |
  new-session-handler.js |     62.5 |      100 |    33.33 |     62.5 |         7,8,14 |
  start-handler.js       |    45.45 |      100 |        0 |    45.45 |8,9,12,15,18,21 |
 dist/intents/           |      100 |      100 |      100 |      100 |                |
  help-intent.js         |      100 |      100 |      100 |      100 |                |
  intent-base.js         |      100 |      100 |      100 |      100 |                |
 dist/models/            |    28.57 |        0 |       50 |    28.57 |                |
  news-repository.js     |    28.57 |        0 |       50 |    28.57 | 20,25,26,27,30 |
 dist/utterances/        |     87.1 |    90.91 |       90 |      100 |                |
  first-utterance.js     |    84.62 |    90.91 |    88.89 |      100 |                |
  language-strings.js    |      100 |      100 |      100 |      100 |                |
  utterance-base.js      |      100 |      100 |      100 |      100 |                |
-------------------------|----------|----------|----------|----------|----------------|
All files                |    65.79 |    61.54 |    42.86 |    72.45 |                |
-------------------------|----------|----------|----------|----------|----------------|


=============================== Coverage summary ===============================
Statements   : 65.79% ( 75/114 )
Branches     : 61.54% ( 16/26 )
Functions    : 42.86% ( 15/35 )
Lines        : 72.45% ( 71/98 )
================================================================================

前回までの投稿では、一部の機能のテストしか行っていない為、以上の結果となりました。

まとめ

カバレッジ率を計測することで、実装コードをどれくらいテストしたか・まだテストしきれていないコードがどのくらいあるのかが判断しやすくなります。
また、隈なくテストをすることでテストの質を良くし、品質を図る指標にも使えると思います。

今回のソースは以下にあります。
カバレッジ

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1