Skip to Content
Pythonによるテスト駆動開発, 3rd Edition
book

Pythonによるテスト駆動開発, 3rd Edition

by Harry Percival
October 2025
Intermediate to advanced
712 pages
9h 46m
Japanese
O'Reilly Media, Inc.
Content preview from Pythonによるテスト駆動開発, 3rd Edition

第16章 より 高度なフォーム

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

より高度なフォームの使用法を見ていこう。 ユーザがリスト項目を空白にしないよう支援できたので、今度は重複も回避できるようにしよう.

これまでの検証制約は空白項目の防止が主目的だった。覚えているかもしれないが、フロントエンドでこれを強制するのは非常に簡単だった。しかし重複項目の防止はフロントエンドでは単純ではない(もちろん不可能ではないが)。だからこの章ではサーバ側検証に重点を置き、バックエンドからUIへエラーを伝播させる手法を扱う。

この章ではDjangoのフォームフレームワークの詳細な仕組みを扱う。 DjangoフォームのカスタマイズやUIへのエラー表示方法が既に理解できている場合、 あるいはDjangoではなくTDDを学ぶために本書を読み取っている場合は、 この章をざっと読み飛ばすことを公式に許可する。

Djangoをまだ学んでいる最中なら、ここには有益な内容がある! ざっと読み取るだけでも構わない。 最後に「開発者のうっかりミスをテストするタイミングについての補足」「まとめ:ビューで何をテストすべきか」は必ず確認しておいてほしい。

重複アイテム用の別のFT

ItemValidationTest に2つ目のテストを追加し、 ユーザがToDoリストに同じ項目を二度入力しようとした時の 動作について少し説明を加える:

src/functional_tests/test_list_item_validation.py (ch16l001)

def test_cannot_add_duplicate_items(self):
    # Edith goes to the home page and starts a new list
    self.browser.get(self.live_server_url)
    self.get_item_input_box().send_keys("Buy wellies")
    self.get_item_input_box().send_keys(Keys.ENTER)
    self.wait_for_row_in_list_table("1: Buy wellies")

    # She accidentally tries to enter a duplicate item
    self.get_item_input_box().send_keys("Buy wellies")
    self.get_item_input_box().send_keys(Keys.ENTER)

    # She sees a helpful error message
    self.wait_for(
        lambda: self.assertEqual(
            self.browser.find_element(By.CSS_SELECTOR, ".invalid-feedback").text,
            "You've already got this in your list",
        )
    )

なぜ一つのテストメソッドを拡張したり、 新しいファイルやクラスを作成したりせずに、 二つ使うのか? これは判断の問題だ。この二つは密接に関連している。 どちらも同じ入力フィールドの検証に関するものだから、 同じファイルにまとめるのが適切に思える。 一方で、論理的には十分に分離されているため、 別々のメソッドに分割するのが現実的だ: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

イーサリアムをマスターする, 2nd Edition

イーサリアムをマスターする, 2nd Edition

Carlo Parisi, Alessandro Mazza, Niccolo Pozzolini, Gavin Wood, Andreas M. Antonopoulos
Excel用Python

Excel用Python

Felix Zumstein

Publisher Resources

ISBN: 0642572274771