2012年7月8日日曜日

アプリケーションのテスト(1)


はじめに

おはようございます。当ブログにアクセス頂き、ありがとうございます。
『やってみせ、言って聞かせて、させてみて、褒めてやらねば人は動かじ』は、少し過保護なのではないかとふと思う、たなけんです。
本エントリでは、アプリケーションのテストを記載いたします。


アプリケーションのテスト(1)

本日は以下のテストコードを紹介します。
  • nsマクロ及び定数
  • 設定ファイル読み込み関数のテスト
  • url構築関数のテスト

nsマクロ及び定数

(ns my-dictionary.core-test
(:use
[clojure.test :only (deftest is)])
(:require
:reload-all
[clojure.test :as test]
[my-dictionary.core :as core])
(:import
(myDictionary.java.AppException)
(java.lang.RuntimeException)))
(def test-url (str
"http://api-pub.dictionary.com/v001?vid="
(:vid core/properties)
"&q=test&type=define&site=dictionary"))
(def test-xml (slurp "test/data/test.xml"))
(def result-string "{\"word\":\"test\",\"entries\":[{\"pos\":\"noun\",\"mean\":\"the means used to determine the quality, content, etc., of something\"},{\"pos\":\"noun\",\"mean\":\"examination to evaluate a student or class\"},{\"pos\":\"verb (used with object)\",\"mean\":\"to subject to a test\"}]}")
view raw core_test.clj hosted with ❤ by GitHub

名前空間および依存ライブラリの宣言をします。
また、テストの検証に用いる定数も宣言します。

設定ファイル読み込み関数のテスト

(deftest test-load-properties
(is (core/load-properties "test/data/test.s")
{:test "test"}))
(deftest test-load-properties-ioe
(is (thrown? myDictionary.java.AppException
(core/load-properties "wrong-file-path"))))
(deftest test-load-properties-re
(is (thrown? java.lang.RuntimeException
(core/load-properties "test/data/test-wrong.s"))))
view raw core_test.clj hosted with ❤ by GitHub

設定ファイル読み込み関数をテストします。エラーの場合は想定されるExceptionかどうか判定します。

  • 正常系: 正しいフォーマットのファイル読み込み (記載内容 {:test "test"})
  • 異常系 1: 設定ファイルが存在しない場合のエラーハンドリング
  • 異常系 2: 設定ファイルの記載内容が異常の場合のエラーハンドリング (記載内容 {:test "test")

url構築関数のテスト

(deftest test-build-url
(is (core/build-url
"api-pub.dictionary.com"
(:vid core/properties)
"test"
"define"
"dictionary")
test-url))
view raw core_test.clj hosted with ❤ by GitHub

引数をつなげた文字列が生成されるかを確認します。

今回の作業は以上。最後までお読み頂きありがとうございました。
たなけん (作業時間30分)


0 件のコメント:

コメントを投稿