TDD with Python (5) - Chapter 4-1

この本も早くも第4章に突入。

Test-Driven Development with Python

1行変えるごとにテストするなんて、時間の無駄じゃね?というツッコミはごもっともで、かく言う自分も本で2回に分けてテストしているのを一気にソース変えてなんてことをやってました。そんな中年のバッタ(w)も思っていたことを作者自体も自問自答しているという告白からスタートしてます。

プログラミングは井戸からバケツで水をくみ上げるようなもの

こういうやり方がスマートじゃない(泥臭い)かもしれないけど、バケツが一杯じゃなかったり、一杯でも井戸が浅ければ大した苦労にはならないけど、開発が進むにつれて、井戸も深くなって、くみ上げるのもどんどん大変になるし、後戻りするのが困難になるはずという指摘は分かる。あまりに単純なところは、こんな一歩一歩やらなくても良いけど、すべてのコードが最低1回はテストされるべきと言うのは守るべきだと。何で書ね、英語で言われると素直に受け入れられるようなw

では前回の続きから

$ python3 manage.py runserver

サーバーを起動してから、テストを起動。

$ 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 20, in test_can_start_a_list_and_retrive_it_later
    self.fail('Finish the test')    #6
AssertionError: Finish the test

----------------------------------------------------------------------
Ran 1 test in 7.680s

FAILED (failures=1)

fail('Finish the test')で落ちているね。前の記事の最後では、サーバーを起動忘れていたみたい。

ここでテスト起動して怒られるパターンなんだろうけど、その前に変更が加わったところをチェック。

  • h1タグのtextを拾って、その中に"To-Do"って文字列が含まれていないかをassertInしてる。
  • id_new_itemという要素を探しに行く。
  • 他にも変更箇所があるが、よく分からない。大丈夫、後で分かるハズだから。

で、怒られに行く。h1タグがないと怒られると予想。

$ python3 functional_test.py
E
======================================================================
ERROR: test_can_start_a_list_and_retrive_it_later (__main__.NewVistorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_test.py", line 21, in test_can_start_a_list_and_retrive_it_later
    header_text = self.browser.find_element_by_tag_name('h1').text
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in find_element_by_tag_name
    return self.find_element(by=By.TAG_NAME, value=name)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"tag name","selector":"h1"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/_g/4qdv4bns0t544w_171sl8jg80000gn/T/tmp3jnb45j_/extensions/fxdriver@googlecode.com/components/driver-component.js:10659)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/_g/4qdv4bns0t544w_171sl8jg80000gn/T/tmp3jnb45j_/extensions/fxdriver@googlecode.com/components/driver-component.js:621)

----------------------------------------------------------------------
Ran 1 test in 10.869s

FAILED (errors=1)

ここでいったんコミットしておく。

$ git diff
$ git commit -am"Functional Test now checks we can iput a to-do item"
[master 52178e9] Functional Test now checks we can iput a to-do item
 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:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

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

    git commit --amend --reset-author

 1 file changed, 22 insertions(+), 6 deletions(-)

(つづく)