LoginSignup
25
19

More than 5 years have passed since last update.

API Blueprintで書いたWeb APIをdreddでテストする

Posted at

概要

さて、今回はAPI Blueprintを使って作成したWeb APIがドキュメント通りに動作するかを、dreddを使ってテストします。

dreddとは

dreddはAPI BlueprintをベースにしたWeb APIのテストフレームワークです。"Language-agnostic"と謳っているだけあってテストのフックにはnode.js以外にもgoやPython、Rubyなど多様な言語が利用でき、またCircleCIやJenkinsCIなどのCIのサポートもあります。

このように、API Blueprintのエコシステムに乗っかることによって、API Blueprint形式で書いた仕様からaglioを使ってドキュメント生成をし、それと同じファイルを使ってWeb APIのテストまで幅広くカバーすることができます。またこれにより、ドキュメントドリブンやテストドリブンといった形でWeb APIを開発することができますし、ドキュメントの書き忘れなどを防ぐことができます。

dreddの初期設定

まずdredd initコマンドでテストの設定をします。対話型のコンソールで、以下の情報を入力します。

  • 対象とするAPI Blueprintの場所とファイル名
  • APIサーバの起動方法
  • APIエンドポイントのURL
  • テストのフックに利用する言語(nodejs,Ruby,Pythonなど)
  • Apiary Test Inspectorを利用するか?
  • CircleCIのconfigファイルを生成するか?
$ dredd init
? Location of the API blueprint recipe.apib
? Command to start API backend server e.g. (bundle exec rails server) python ../../bottle/sample.py
? URL of tested API endpoint http://localhost:8080/
? Programming language of hooks nodejs
? Do you want to use Apiary test inspector? No
? Dredd is best served with Continuous Integration. Create CircleCI config for Dredd? No

Configuration saved to dredd.yml

Run test now, with:

  $ dredd

dreddではみずからAPIサーバの起動してテストを走らせます。今回は前々回の記事で使用したPython+Bottleのコードを利用するため、python ../../bottle/sample.pyのように起動コマンドを指定しています。

これでdreddを実行するためのdredd.ymlが生成されます。

dreddでテストを行う

dredd initで作成した設定ファイルを元に、dreddコマンドでdreddを起動します。

$ dredd
info: Configuration dredd.yml found, ignoring other arguments.
info: Starting server with command: python ../../bottle/sample.py
info: Waiting 3 seconds for server command to start...
warn: Parser warning in file 'recipe.apib': (2) the resource '/recipes/{name}' is already defined on line 31
warn: Parser warning in file 'recipe.apib': (2) the resource '/recipes/{name}' is already defined on line 45
info: Beginning Dredd testing...
pass: GET /recipes/ duration: 42ms
pass: GET /recipes/name duration: 22ms
pass: DELETE /recipes/name duration: 26ms
pass: PUT /recipes/name duration: 28ms
complete: 4 passing, 0 failing, 0 errors, 0 skipped, 4 total
complete: Tests took 126ms
info: Sending SIGTERM to the backend server
info: Backend server was killed

dreddによるテストが終了しました。ここでは無事4つのテストがパスしており、Pythonで書かれたAPIサーバがrecipe.apibに書いたドキュメント通りの動きをしていることが確認できました。

なお、既に上で述べたようにdreddは自分でAPIサーバを起動してテストを行うため、すでに別でAPIサーバを起動している場合はエンドポイントURLが重複してdreddがエラーとなる場合があります。そのため、通常はhttp://localhost:8080/で起動するAPIサーバの場合、dreddに登録する設定はhttp://localhost:8081/のようにした上で、APIサーバもdredd専用のportで起動するなどしたほうがいいかもしれません。

Apiary Test Inspectorについて

dredd initでApiary Test Inspectorの使用を"Yes"にすると、dredd起動時に以下のようなURLが出力されます。これはdreddの結果をApiary.ioのPublic Test Runにアップロードされ一時的にテストの結果を共有する機能で、ウェブでテスト結果を確認することができます。

complete: See results in Apiary at: https://app.apiary.io/public/tests/run/0973d151-0c39-43ca-9336-6bb1c17a8ddd

ss_2016-04-23_16_05_11.png

通常Apiaryを利用する際は、アカウントを作成したでGithubのレポジトリに上げた.apibファイルと連携といった形でテスト結果を管理することができますが、一時的に利用したいだけの場合などはApiary Test Inspectorを用いた共有もできます。

参考

25
19
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
25
19