moritetuのIT関連技術メモ

cram

pythonで書かれた、コマンドラインベースのプログラムのテストに有用なテストツール。
特徴的なのは、実行コマンドと結果をターミナル画面のような形式で記述するところである。
これの何が良いのかというと、テストファイルを見れば、どのようなコマンドを実行して、どのような結果となるのか、直感的に判断することができる、という点である。

例1 sample.t

$ cat tests.t
test echo:

  $ echo foo
  foo

test expr:

  $ expr 1 + 1
  10

実行すると以下のようになる。

$ cram tests.t
!
--- tests.t
+++ tests.t.err
@@ -6,4 +6,4 @@
 test expr:

   $ expr 1 + 1
-  10
+  2

# Ran 1 tests, 0 skipped, 1 failed.

と、上記のとおり、
echo fooの結果がfoo、
expr 1 + 1の結果が10(誤りで正しくは2)
であることが、テストファイルを見ればすぐに分かる。

インストール

インストールは簡単であり、pipまたはmakeで可能。

pip install cram

or

git clone https://github.com/brodie/cram.git
cd cram
make install

テスト

テストプログラム

その他の記述はコメントとして解釈されるので、テストに関する説明を自由に記述できる。
インデントでfocusされた箇所が、ターミナルの画面をそのまま投影したような感じであることが分かるだろう。

テストの実行結果

テストの実行結果について、柔軟なAssertが可能なように、cram独自のシンタックスが用意されている。

(re)

正規表現でマッチすることを示す。

$ cat re.t
Re test:

  $ echo "hello world"
  hello .* (re)
$ cram re.t
.
# Ran 1 tests, 0 skipped, 0 failed.

(glob)

glob形式でマッチすることを示す。

$ cat glob.t
glob test:

  $ touch hoge.txt
  $ ls hoge.txt
  *.txt (glob)

$ cram glob.t
.
# Ran 1 tests, 0 skipped, 0 failed.

(esc)

改行などの制御系の文字を含む行のマッチ。

$ cat esc.t
esc test:

  $ printf "%s\r\n" "hello world"
  hello world\r (esc)
$ cram esc.t
.
# Ran 1 tests, 0 skipped, 0 failed.

(no-eol)

改行区切りでない文字列のマッチ。

$ cat no-eol.t
no-eol test:

  $ printf "%s" "hello world"
  hello world (no-eol)
$ cram no-eol.t
.
# Ran 1 tests, 0 skipped, 0 failed.

参考リンク


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP