- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2020-02-11T21:51:22+09:00","default:haikikyou","haikikyou")
#author("2020-02-12T00:56:42+09:00","default:haikikyou","haikikyou")
[[moritetuのIT関連技術メモ]]
#contents
* cram [#sea1d124]
pythonで書かれた、コマンドラインベースのプログラムのテストに有用なテストツール。~
特徴的なのは、実行コマンドと結果をターミナル画面のような形式で記述するところである。~
これの何が良いのかというと、テストファイルを見れば、どのようなコマンドを実行して、どのような結果となるのか、直感的に判断することができる、という点である。
&label(sample){例1}; ''sample.t''
#geshi(bash){{{
$ cat tests.t
test echo:
$ echo foo
foo
test expr:
$ expr 1 + 1
10
}}}
実行すると以下のようになる。
#geshi(bash){{{
$ 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.
}}}
と、上記のとおり、~
&code(){echo foo};の結果がfoo、~
&code(){expr 1 + 1};の結果が10(誤りで正しくは2)~
であることが、テストファイルを見ればすぐに分かる。
* インストール [#ke81640f]
インストールは簡単であり、&code(){pip};または&code(){make};で可能。
#geshi{{{
pip install cram
}}}
or
#geshi{{{
git clone https://github.com/brodie/cram.git
cd cram
make
make install
}}}
* テスト [#e3330d7e]
** テストプログラム [#w49e740a]
- テストファイルは、拡張子&code(){.t};で作成する。~
ファイル指定であれば、&code(){.t};でなくともよいが、ディレクトリ指定で複数のテストプログラムを実行する場合は、&code(){.t};であることを期待しているので、&code(){.t};が良い。
- スぺース2つ以上で、$(ドルサイン)で始まる行がコマンドとして解釈される。
- 複数行のコマンドの場合は、シェルのように&code(){ > };で続きのコマンドを記述する。
#geshi{{{
# Comment
You can write anything here.
For example, the description of the test and so on:
$ echo "foo" \
> "bar"
foo bar
}}}
- 期待結果がある場合は、同様にインデントしてコマンド行の続きに記述する。
その他の記述はコメントとして解釈されるので、テストに関する説明を自由に記述できる。~
インデントでfocusされた箇所が、ターミナルの画面をそのまま投影したような感じであることが分かるだろう。
** テストの実行結果 [#qfb86c81]
テストの実行結果について、柔軟なAssertが可能なように、cram独自のシンタックスが用意されている。
*** (re) [#s655c422]
正規表現でマッチすることを示す。
#geshi(bash){{{
$ cat re.t
Re test:
$ echo "hello world"
hello .* (re)
$ cram re.t
.
# Ran 1 tests, 0 skipped, 0 failed.
}}}
*** (glob) [#qee60245]
glob形式でマッチすることを示す。
#geshi(bash){{{
$ cat glob.t
glob test:
$ touch hoge.txt
$ ls hoge.txt
*.txt (glob)
$ cram glob.t
.
# Ran 1 tests, 0 skipped, 0 failed.
}}}
*** (esc) [#x11db69c]
改行などの制御系の文字を含む行のマッチ。
#geshi(bash){{{
$ 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) [#x9e29d56]
改行区切りでない文字列のマッチ。
#geshi(bash){{{
$ 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.
}}}
* 参考リンク [#g7ace6eb]
- https://github.com/brodie/cram
- https://pypi.org/project/cram/