TDD with Python (2) - Chapter 2

時間があるうちに進めちゃおうということで次に行きます。このチュートリアルではto-do listを作っていくそうです。

Test-Driven Development with Python

Test-Driven Development with Python

Functional Testとは

Functional Testというのは、人が読んで分かるUser storyが含まれているべきと言うのが作者の主張なんですね。必要最低限のところから始めるMVPから使える製品になるまで改善を進めていくというイメージに沿ってこの本でも進めていくらしい。

Minimum viable product - Wikipedia, the free encyclopedia

よって、テストを書くときはいつもコメントから書き始めるそうな。

だから、こうなるのね。本文よりコメント長いプログラムを初めて書いたよw。コメントに書くのは動作そのものではなくて、なんでこういうロジックにするのかというwhyを書くと良いらしいというのは、諸々の本でも見かけるよね。なにげにソースもちらっと変わっているので、実験してみましょ。

$ python3 manage.py runserver

からの

$ python3 functional_test.py
Traceback (most recent call last):
  File "functional_test.py", line 10, in <module>
    assert "To-Do" in browser.title
AssertionError

はいエラー。assertのところを書き換えたから当たり前だね。

ただ、unittestという、もうちょっと便利なモジュールがあるからそれを使おうねと言うことで書き換え。

unittest自体は、以前読んだ入門 Python 3の第12章の中で簡単に紹介されていたので、まったく初見という訳ではない。setUp()はテストの前さばきとして各テストケースの実行前に呼び出される。この場合は、ブラウザはFirefoxを使ってねという指定をしている。tearDown()は、その逆で後始末。今回はブラウザを閉じてねという指示を出している。それ以外のメソッドがテストケースになる訳だね。

assertInは、組み込み関数で、'To-do" in self.browser.titleと同じ判定をしている。似たような関数はヘルプに書いてある。
26.4. unittest — ユニットテストフレームワーク — Python 3.5.1 ドキュメント
いや、こっちの方が良いな。
26.4. unittest — ユニットテストフレームワーク — Python 3.5.1 ドキュメント

self.failは、テストの強制終了ってところかな。引数の内容をエラーメッセージとして出力しますと。

もちろんサーバーが走った状態で実行してみる。

$ python3 functional_test.py
F
======================================================================
FAIL: test_can_start_a_list_and_retrive_it_later (__main__.NewVistorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_test.py", line 18, in test_can_start_a_list_and_retrive_it_later
    self.assertIn("To-Do", self.browser.title)   #5
AssertionError: 'To-Do' not found in 'Welcome to Django'

----------------------------------------------------------------------
Ran 1 test in 7.084s

FAILED (failures=1)

これを見る限り、self.failが呼ばれてない気もするけど。。。1つのテストが実行されて、そのうち失敗が1個ということが分かる。

とりあえず第2章の内容は終わったので、git commit -aする。

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   .gitignore
	modified:   functional_test.py

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
...(中略)...
$ git commit -a
[master 42c68f5] Implemented improvements in Chapter 2
 Committer: Ken <ken@Kens-Macbook-Air-2010.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 2 files changed, 46 insertions(+), 6 deletions(-)
 rewrite functional_test.py (73%)

(たぶんつづく)