What is TDD?
If somebody asks me to explain TDD in few words, I say TDD is a development of tests before a feature implementation. You can argue that it’s hard to test things which are not existing yet. And Kent Beck will probably give you a slap for this.
So how is it possible? It can be described by following steps:
1. You read and understand requirements for a particular feature.
2. You develop a set of tests which check the feature. All of the tests are red, due to absence of the feature implementation.
3. You develop the feature until all tests become green.
4. Refactor the code.
TDD requires a different way of thinking, so in order to start TDD you need to forget the way you developed code before. This process is very hard. And it is even harder if you don’t know how to write unit tests. But it’s worth it.
Developing with TDD has valuable advantages:
1. You have a better understanding of a feature you implement.
2. You have robust indicators of feature completeness.
3. Code is covered with tests and has less chance to be corrupted by fixes or new features.
The cost of these advantages is pretty high – inconvenience related to switching to a new development manner and time which you spend for developing each new feature. It’s a price of quality.
So that’s how TDD works – write red unit tests, start implementing a feature, make the tests green, and refactor the code.
So how is it possible? It can be described by following steps:
1. You read and understand requirements for a particular feature.
2. You develop a set of tests which check the feature. All of the tests are red, due to absence of the feature implementation.
3. You develop the feature until all tests become green.
4. Refactor the code.
TDD requires a different way of thinking, so in order to start TDD you need to forget the way you developed code before. This process is very hard. And it is even harder if you don’t know how to write unit tests. But it’s worth it.
Developing with TDD has valuable advantages:
1. You have a better understanding of a feature you implement.
2. You have robust indicators of feature completeness.
3. Code is covered with tests and has less chance to be corrupted by fixes or new features.
The cost of these advantages is pretty high – inconvenience related to switching to a new development manner and time which you spend for developing each new feature. It’s a price of quality.
So that’s how TDD works – write red unit tests, start implementing a feature, make the tests green, and refactor the code.
Place of Unit Tests in TDD
Since unit tests are the smallest elements in the test automation pyramid, TDD is based on them. With the help of unit tests we can check the business logic of any class. Writing unit tests is easy if you know how to do this. So what do you test with unit tests, and how do you do it? I’ll try to illustrate answers in a concise form.
I have created two repositories based on unit testing with Mockito and TestNG / JUnit in GitHub. Refer them if you want to know how testing works.
~ Chathumina Vimukthi
Comments
Post a Comment