Category Archives: Code

How to Develop Test Code in Test-Driven Development


The reference book is Test Driven Development By Example [ Kent Beck ], and the Japanese version is テスト駆動開発入門.

This time, let’s talk about the essence of testing. I wrote down the important points while writing tests, implementing code, and adding functionality. This is a continuation of the procedure for writing tests first in test-driven development.

Write Asserts First

When writing tests, it seems best to start with Asserts. Indeed, starting from the goal is a common practice.

When starting with Assert, the first two things to consider are:

  • What is the right answer? (What makes it correct?)
  • How am I going to check it? (How do I check it, and what value do I check?)

Looking at the examples in the book, you can see that they start with these two points, write tests using non-existent classes from the Assert part, and finally complete the tests.

Test Data

According to Kent Beck, there are also rules for the data used in tests. It is said that easily understandable data, such as 1 or 2, should be used if possible.

Also, depending on the system, real data may be used in testing. Kent Beck mentions that real data is particularly effective in the following cases:

  • When testing real-time systems that use data output from other systems in operation.
  • When comparing output between the program before the change and the program after the change.

It’s not a particularly difficult topic, but let’s organize it in our minds for now.

Rules for Implementing Tests

Test code may deviate from general programming rules. For example, magic numbers. This is because the purpose of testing is to complete the implementation code and obtain feedback from the test as needed. As mentioned in the “MONEY EXAMPLE” chapter, they initially completed the test by using magic numbers.

The methods enumerated in the “MONEY EXAMPLE” chapter are explained again. There are “Fake It” and “Triangulation,” but the one you must understand first is “Fake It.”

Fake It (‘Til You Make It)

An example from when a testing framework was created in Python is introduced on page 102 of the English version. The part where the test code gradually changes from being fake is reprinted.

Kent Beck lists the following two merits of “Fake It.” The second one was also mentioned a little earlier.

  • First, by making the test green, you can be confident in your code. You don’t have to worry unnecessarily.
  • Even if it’s obvious, by writing tests and making them green, you can easily see what functionality is currently lacking.

From there, the discussion continues to “Triangulation.” Since it’s a review of the story that appeared in the “MONEY EXAMPLE” section, I’ll skip it.

Adding Parameters

There is an example of how to write test code when adding functionality after the test is completed, such as changing from sum(int value) to sum(int[] values). Of course, it’s an artificially created method, so there are some unnatural parts, but you should grasp the overall flow.


テスト駆動開発でテストを先に書く手順


Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う

参考書籍は Test-Driven Development By Example、 日本語版は テスト駆動開発入門です。

今回読むのは Section III のところです。 テスト駆動開発のパターン について書かれています。

Isolated Test

Test の独立性についてですが、 Kent Beck は過去の経験から次のように綴っています。

First, make the tests so fast to run that I can run them myself, and run them often. That way I can catch errors before anyone else sees them, and I don’t have to dread coming in in the morning. Second, I noticed after a while that a huge stack of paper didn’t usually mean a huge list of problems. More often it meant that one test had broken early, leaving the system in an unpredictable state for the next test.

続けて書かれていますが、 ひとつの問題が発生した場合に失敗するテストはひとつであるべきです。 問題の特定を楽にするために。

そんな独立性の高いテストを作る手頃な方法として、 “実行順序に依存しないテスト” が挙げられていますね。

テストを書く前に ToDo List

Kent Beck は彼の経験から、 目的を達成するためには (プログラムで実現すべきことを実現するためには) やることを全部書き出すというのがいい と結論づけています。 Money Example の中でも ToDo List を作っていました。

ToDo List のメンテナンスにも注意が必要です。 Kent Beck は新しい ToDo が増えたとき、 既存の ToDo 含め 次の3つのカテゴリに分けます。

  • いまやる
  • あとでやる
  • できなくてもいい

テスト駆動開発において ToDo List に書かれるのは Test として実装するべき事項です。 具体的には Section IMoney Example に載っていたものです。

ToDo の書き方

Kent Beck は 大変丁寧なことに、 ToDo の書き方まで説明してくれています。 これは早く実践していきましょう。

  • 考察が必要な項目については “???” をつける。
  • すぐに実装に取りかかれるように書く。
    1. 実装が必要な演算についてテストケースを書く。
    2. もし実装前の演算であれば、 その演算の null version (特殊な場合) のテストケースを書く。
    3. 必要だと思うリファクタリングをリストアップする。

なぜテストを先に書くのか

Kent Beck はテストを先に書く理由について、 次のように書いています。

  • コードを書くプログラマとしてのゴールは機能追加であってテストを書くことではないため、実装の後(目的達成後)にテストコードを書くことはない。 (目的達成後にテストを書こうと思わない。)
  • (コードの)デザインについて考える時間が必要。
  • スコープを制御するためのメソッドが必要。 ここでいうスコープは、後のページで詳しく説明が出てくるが、要するにテストを順次グリーンにして、今実装すべき機能(スコープ)を整理するということ。

確かに、 実装が先に終わってテストがおまけになってしまう、 そんな経験が多々あります。 更に Kent Beck は テストのモチベーションのサイクルについても言及しています。 テストのモチベーションは開発者にとって一番大事かもしれません。

Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う

テスト駆動開発で使う テストフレームワークの仕組み


Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う

参考書籍は Test-Driven Development By Example、 日本語版は テスト駆動開発入門です。

Money Example の次は jUnit のようなテストツールを作ってみるお話です。 30以上の言語で使われている xUnit は、 今や巨大な資産ですが、 そういったものは自分で作れるものなんでしょうか。

テストフレームワークの仕組み

読んでみると、実際にやっていることは難しくなさそうです。 肝となるのは 一連のテストの結果 (実行・成功・失敗したテスト数) をどうやって保持するかではないかと思いました。 独自にテストフレームワークを作るときに役に立ちそうです。

社内でも独自にテストフレームワークを作っている人がいます、 かなりシンプルにできるそうです。 その人によると、 PHPUnit などのテストフレームワークは いろんなファンクションや機能をつけているから複雑になっているそうです。 そうじゃないと使ってもらえないからだとも言っていましたが。

JUnit や PHPUnit は TestCase だったかのクラスを継承してテストコードを書いていくので 今回説明されていたやり方と同じですね。 でも rspec だと describe で書き始めたりするし クラス の 定義 で始めるわけでもないので 本書とは別のアプローチかもしれません。

Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う

テスト駆動開発でテストを書かない場合がある


Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う

参考書籍は Test-Driven Development By Example、 日本語版は テスト駆動開発入門です。

機能追加の方針

Franc-ly Speaking のところ(29ページ)からです。 Franc を導入して複数通貨に対応させるんですね。 39ページに至るまでの流れを見ていくと次のようになります。

  1. Dollar を複製する。 テストコードも複製する。
  2. 親クラス Money と 継承クラス Dollar, Franc に分ける。
  3. Factory Method PatternMoney クラス に共通のものを集約する。

Dollar から Money へ、 子クラスから親クラスへメソッドを移す手順には要注意ですね。

書かないテスト

途中、 toString() というメソッドを、テストコードを書かずに追加しています。 この判断基準は重要なのでまとめておきます。

次の条件を満たす場合は、テストコードを書かなくてもOKとします。

  • 画面上で(テストの)結果を見ようとするメソッドを追加しようとしている。
  • 追加するコードはデバッグのためのコードで、仮に正常に動かなくなったとしてもダメージが小さい。
  • テストが Red で終了している。 (Red のときは新しいテストコードを書きたくない。)

普遍的にまとめた記述が今のところ出てきていないので toString() 追加の際の説明をまとめたものになっています。

toString() 程度 だったらテストを書いてもよさそうですが、 もっと複雑なものに成った場合には テストを書かずに進めた方がいいんでしょうか。 これは実践あるのみですね。

その後 Money Example の演算の実装と、 全体を俯瞰した傾向、 テストの品質について重要な記述がありますが、 ひとつひとつ紹介すると長くなるので割愛します。 でもひとつだけ気になる記述があるので紹介します。

Easy to read for programmers – Unsynchronized documentation is scarce. The tests will be more valuable if they are readable, giving an interesting second perspective on the messages hidden in the source code.

テストを書けば仕様も理解しやすくなるかもしれないと思いました。

Test-Driven Development By Example (Kent Beck)
Kent Beck の Test-Driven Development By Example を Amazon で買う