#author("2020-02-12T00:56:42+09:00","default:haikikyou","haikikyou")
#author("2020-02-16T22:54:36+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 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された箇所が、ターミナルの画面をそのまま投影したような感じであることが分かるだろう。

&label(memo){メモ}; ''>が結果に含まれるテスト結果の記述''

curlの-vオプション付きの結果を比較しようとするとやや面倒である。~
&code(){>};の記号がリクエストヘッダーの入力と一致するためである。~
この場合は、sedコマンドなどで置換して結果を作成する必要がある。

#geshi{{{
curl test:

  $ curl -s -vLo- "http://localhost/" |& sed -e 's/>/!/g'
  !
  * About to connect() to localhost port 80 (#0)
  *   Trying ::1...
  * Connected to localhost (::1) port 80 (#0)
  ! GET / HTTP/1.1\r (esc)
  ! User-Agent: curl/7.29.0\r (esc)
  ! Host: localhost\r (esc)
  ! Accept: */*\r (esc)
  ! \r (esc)
  < HTTP/1.1 403 Forbidden\r (esc)
  < Date: Sun, 16 Feb 2020 13:51:34 GMT\r (esc)
  < Server: Apache/2.4.6 (CentOS) PHP/7.3.14\r (esc)
  < Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT\r (esc)
  < ETag: "1321-5058a1e728280"\r (esc)
  < Accept-Ranges: bytes\r (esc)
  < Content-Length: 4897\r (esc)
  < Content-Type: text/html; charset=UTF-8\r (esc)
  < \r (esc)
  { [data not shown]
  (省略)
}}}
** テストの実行結果 [#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/

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