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

第13章 テストを複数のファイルに分割する、 そして汎用的な待機ヘルパー

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

ローカル開発に戻る! 次に実装したい機能は、簡単な入力検証だ。 しかし新しいテストを書き始めると、functional_tests.pytests.py といった単一のファイル内で方向感覚を失い始めることに気づくだろう。そこで複数のファイルに再編成する——いわばテストの小さなリファクタリングだ。

また汎用的な明示的待機ヘルパーも構築する。

検証機能の開発開始:空白項目の防止

最初の数人のユーザがサイトを使い始めたところ、 リストを混乱させるミスを時々犯していることに気づいた。 例えば、誤って空のリスト項目を送信したり、 リストに全く同じ項目を2つ入力したりするのだ。 コンピュータは人間が愚かなミスを犯すのを防ぐためにある。 だから、サイトにその手助けをさせられないか試してみよう。

新しいFTメソッドの概要は次の通りだ。これをNewVisitorTestCase に追加する:

src/functional_tests/テスト.py (ch13l001)

def test_cannot_add_empty_list_items(self):
    # Edith goes to the home page and accidentally tries to submit
    # an empty list item. She hits Enter on the empty input box

    # The home page refreshes, and there is an error message saying
    # that list items cannot be blank

    # She tries again with some text for the item, which now works

    # Perversely, she now decides to submit a second blank list item

    # She receives a similar warning on the list page

    # And she can correct it by filling some text in
    self.fail("write me!")

それは結構なことだが、先に進む前に—— 機能テスト(FT)ファイルが少し混雑し始めている。 複数のファイルに分割しよう。各ファイルには単一のテストメソッドを置く。

FTは「ユーザストーリー」や機能と密接に関連していることを忘れないでほしい。 FTを整理する一つの方法は、上位レベルの機能ごとに1つずつ用意することだ。

また、全てのテストが継承できるベーステストクラスも用意する。 その手順を段階的に説明する。

テストのスキップ

注記

ローカル開発環境に戻った。 ターミナルで次のコマンドを実行し、TEST_SERVER 環境変数が解除されていることを確認せよ:unset TEST_SERVER

リファクタリング時には、テストスイートが完全にパスしている状態が望ましい。 我々は意図的に失敗するテストを書いたばかりだ。 unittest の「skip」デコレータを使って、一時的にこのテストを無効にしよう:

src/functional_tests/テスト.py ...

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