O'Reilly logo

Test Driven Development: By Example by Kent Beck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix II. Fibonacci

In answer to a question from one of the reviewers of this book, I posted a test-driven Fibonacci. Several reviewers commented that this example turned on their light about how TDD works. However, it is not long enough, nor does it demonstrate enough of TDD techniques, to replace the existing examples. If your lights are still dark after reading the main examples, take a look here and see.

The first test shows that fib(0) = 0. The implementation returns a constant.

public void testFibonacci() {
					assertEquals(0, fib(0));
					}

int fib(int n) {
   return 0;
					}
				

(I am just using the TestCase class as a home for the code, because it is only a single function.)

The second test shows that fib(1) = 1.

public void testFibonacci() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required