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

第4章 これだけのテストを何のために やっているのか?(そしてリファクタリング)

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

TDDの基本を実際に動かしてみせたところで、 一旦立ち止まって、なぜこれを行うのかを話そう。

読者の諸君の中には、今まさに 抑えきれない苛立ちを抱えている者もいるだろう。 もしかすると、以前少しだけ単体テストをしたことがある者もいれば、 単に急いでいる者もいるかもしれない。 君たちはこんな疑問を飲み込んできたはずだ:

  • これらのテストは少し過剰ではないか?

  • 確かにいくつかは重複しているのでは? 機能テストと単体テストの間で重複がある。

  • あの単体テストは明らかに些細すぎる——定数を返す一行関数をテストするなんて! 時間の無駄じゃないか? もっと複雑なことにテストを集中させるべきじゃないのか?

  • 単体テストとコードの往復で生じる細かい変更はどうするんだ? 最初から最後まで一気に進められないのか?つまり、home_page = None !?マジかよ?

  • まさか、実際にこんな風にコードを書いてるわけじゃないだろうな?

ああ、若き弟子よ。私もかつては同じ疑問に満ちていた。 だがそれは、それらが正当な疑問だからに他ならない。 実際、今でも私は常にこうした疑問を抱いている。 これらは本当に価値があるのか? 単なるカーゴカルトではないのか?

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

結局のところ、プログラミングは難しい。多くの場合、我々は賢いから成功する。 TDDは、我々がそれほど賢くない時に助けてくれるものだ。 TDDを実質的に発明したケント・ベックは、ロープで井戸からバケツの水を引き上げるという比喩を使っている:井戸が深すぎず、バケツがあまりいっぱいじゃない時は簡単だ。 満杯のバケツでも、最初はかなり楽に上げられる。 だがしばらくすると、疲れてくる。 TDDは、進捗を保存できるラチェットのようなものだ。 休憩を取っても、決して後退することはない。

そうすれば、常に頭を使う必要はない(図4-1参照)。

Test ALL the things
図4-1. あらゆるものをテストせよ Allie Brosh『Hyperbole and a Half』より改変)

さて、おそらく一般性ではTDDが良い考えだと認めるだろう。 それでもまだ、俺がやりすぎだと思っているかもしれない? ごく小さなことをテストし、途方もなく多くの小さなステップを踏むなんて?

TDDは訓練だ。つまり自然につくものではない。 多くの見返りは即座には得られず、長期的にしか現れないから、 その瞬間に自らを強制しなければならない。 それが「テストヤギ」のイメージが表すものだ—— そこには少し頑固な意志が必要だ。

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