How to get Started with TDD
November 17th, 2009 | Published in Google Testing
By Miško Hevery
Best way to learn TDD is to have someone show you while pairing with you. Short of that, I have set up an eclipse project for you where you can give it a try:
Your mission is to make the calculator work using TDD. This is the simplest form of TDD where you don't have to mock classes or create complex interactions, so it should be a good start for beginners.
TDD means:
I have already done all of the work of separating the behavior from the UI, so that the code is testable and properly Dependency Injected, so you don't have to worry about running into testability issues.
I have started you off with first 'testItShouldInitializeToZero' test. Here are some ideas for next tests you may want to write.
I would love to see what you will come up with and what your thoughts are, after you get the whole calculator working. I would also encourage you to post interesting corner case tests here for others to incorporate. If you want to share your code with others, I would be happy to post your solutions.
Good luck!
PS: I know it is trivial example, but you need to start someplace.
Best way to learn TDD is to have someone show you while pairing with you. Short of that, I have set up an eclipse project for you where you can give it a try:
-
hg clone https://bitbucket.org/misko/misko-hevery-blog/
- Open project blog/tdd/01_Calculator in Eclipse.
- It should be set up to run all tests every time you modify a file.
- You may have to change the path to java if you are not an Mac OS.
- Project -> Properties -> Builders -> Test -> Edit
- Change location to your java
- Right-click on Calculator.java -> Run As -> Java Application to run the calculator
Your mission is to make the calculator work using TDD. This is the simplest form of TDD where you don't have to mock classes or create complex interactions, so it should be a good start for beginners.
TDD means:
- write a simple test, and assert something interesting in it
- implement just enough to make that tests green (nothing more, or you will get ahead of your tests)
- then write another test, rinse, and repeat.
I have already done all of the work of separating the behavior from the UI, so that the code is testable and properly Dependency Injected, so you don't have to worry about running into testability issues.
- Calculator.java: This is the main method and it is where all of the wiring is happening.
- CalculatorView.java: This is a view and we don't usually bother unit testing it has cyclomatic complexity of one, hence there is no logic. It either works or does not. Views are usually good candidates for end-to-end testing, which is not part of this exercise.
- CalculatorModel.java: is just a PoJo which marshals data from the Controller to the View, not much to test here.
- CalculatorController.java: is where all of your if statements will reside, and we need good tests for it.
I have started you off with first 'testItShouldInitializeToZero' test. Here are some ideas for next tests you may want to write.
- testItShouldConcatinateNumberPresses
- testItShouldSupportDecimalPoint
- testItShouldIgnoreSecondDecimalPoint
- testItShouldAddTwoIntegers
I would love to see what you will come up with and what your thoughts are, after you get the whole calculator working. I would also encourage you to post interesting corner case tests here for others to incorporate. If you want to share your code with others, I would be happy to post your solutions.
Good luck!
PS: I know it is trivial example, but you need to start someplace.