DEV Community

Introduction to TDD: What it is and why it is important

Hannah on August 04, 2020

Many companies today are using test driven development or TDD to create their web software. Why is test driven development so important?...
Collapse
 
n_develop profile image
Lars Richter

Hi Hannah,
Hi Andy,

Yes, I think "Refactor" should be an explicit step. Otherwise it is ignored too often.
I'm also not the biggest fan of the popular "Red - Green - Refactor" cycle. Mainly, because it is missing another important explicit step. Kent Beck originally wrote the following in his famous book:

  1. Add a little test
  2. Watch it fail
  3. Make a little change
  4. Run Tests and succeed
  5. Refactor to remove duplication

The second step is super important and I am happy that Hannah mentioned it in this post. Watching the test fail should be an explicit step in every TDD cycle as well.

Collapse
 
alvesvalentin profile image
Alvès Valentin

I was thinking about the same thing, in the beginning i was coding with TDD i was always telling to myself the different steps (like in the image) i was like red, green, purple, red green, purple... my coworkers thought i was crazy (well they're not wrong xD) I like that sentence in your article "help teams communicate the intentions of their code's" it is true, sometimes i write a test for "documentary" purpose but don't forget refactoring can help your team to understand your code :)

overall nice article though :)

(image found on google: "test driven development circle" found it from the site lewandowski.io/2017/02/thre-levels...

Thread Thread
 
wolfdominion profile image
Hannah

Thanks for sharing the article, the image is very concise.
I can relate to the thinking of steps in colors, having an art background a lot of the way I code is very visual in my mind.

Thread Thread
 
alvesvalentin profile image
Alvès Valentin

you're welcome ! :D I'm more a "talk-to-myself" kind of person and moving a lot in the office it .... can annoy some colleagues xD

Collapse
 
wolfdominion profile image
Hannah

This is interesting because I was debating about specifying about the "Red - Green - Refactor" in the article. However, I felt that it narrowed down the essence of TDD. If someone is new to the concept, I don't want them to think this is the main or 'only' way.

I really like the Kent Beck reference. Which book is this from? I'm interested in reading it.

Thread Thread
 
n_develop profile image
Lars Richter

It's from Test-Driven Development By Example.
The book is absolutely worth reading. And with its 240 pages, it is relatively short. :-)

Collapse
 
xtofl profile image
xtofl

I even used to push the failing test, to have the CI prove I have a test I can make run.

Collapse
 
btlm profile image
btlm

In my last job, we wrote tests to existing code. One authority said then that is like foreplay after having sex. I wonder why he assumed developers know how to have sex, but now I know how to write tests.

Collapse
 
wolfdominion profile image
Hannah

Hahah, developers don't have time for that. We have to sit at the computer all day with our unromantic 'logic'

That's weird though, did a non-programming manager decide you all had to write tests for pre-existing code?

Collapse
 
btlm profile image
btlm

Exactly. We had a boss (a small company's CEO) who once heard a new term TDD and wanted us to implement it in our existing project because he thought it would help us deliver things faster 🤔 (it didn't)

Collapse
 
bugf1nd3r profile image
bugf1nd3r • Edited

thanks for sharing. the actual problem starts how to write those test efficiently since most of time it connects to DB.. so with mocking DB or other function/API response that is when the actual problem starts and developer loose zeal of writing proper test cases

Collapse
 
wolfdominion profile image
Hannah

You're welcome! You are right, the next step is to actually do the hard work- coding and coding it well. I am working on learning Jest testing right now, so hopefully I'll get some more technical how-to posts up in the next month or two.

Collapse
 
pentacular profile image
pentacular

I think you skipped the "why it is important" section.

Collapse
 
n_develop profile image
Lars Richter

I think Hannah did answer the question:

It prevents issues and bugs in the future, it can increase the efficiency of your work flow, and help teams communicate the intentions of their code's functionality.

Collapse
 
pentacular profile image
pentacular

I guess I missed it since it was so small and labeled "Why is test driven development so popular?" :)

Thread Thread
 
wolfdominion profile image
Hannah

Thanks for mentioning this, I updated the article to improve the readability!

Collapse
 
ucefidel profile image
Youssef Idelhadj

A huge article, thanks to share with us your knowledge

Collapse
 
wolfdominion profile image
Hannah

You're welcome! I had no idea people would find it so useful, I was just trying to learn it myself haha

Collapse
 
wolfdominion profile image
Hannah

I'm so glad you added to this. It's definitely not just about getting the test to pass, but also making sure it is efficient.