DEV Community

Cover image for 4 Reasons You Should Write Tests First

4 Reasons You Should Write Tests First

Milecia on May 30, 2019

Tests are met with varying opinions. Some developers hate writing tests and some don't mind it. There are a few key benefits you get when you write...
Collapse
 
victorosilva profile image
Victor Silva • Edited

I started writing tests before code about one year ago. Now I can't live without it.

The Test Driven Development approach makes so much more productive and gives me so much more confidence that I wish I had started using it earlier.

Collapse
 
hoggworks profile image
Brian Hogg

Man, I wish I had time to do TDD work, but the pace I need to make my deadlines won't fit.

(I've done TDD with some personal projects and enjoyed it despite working feeling sooooo slooooow, but in my day job I'm maintaining and adding to a large old codebase with no tests, because, man. No time.)

Collapse
 
briandesousa1 profile image
Brian De Sousa

So true. TDD for greenfield projects sounds great. TDD for projects making enhancements on large legacy code bases that have little or no tests is much more difficult!

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

"DDT Development Driven Tests" are the stuff of nightmares.

Collapse
 
dmelidonis profile image
Dimitrios Melidonis • Edited

I started applying TDD at work 9 years ago. I though it was worth the extra effort for all the reasons you mention and some more. So at first I was a big advocate of "Test first" like you. At some point though I had to give up.

I stopped writing test and for the last 3 years I have been much happier and more productive without them. I had written off TDD until I came across this tweet:

About 6 months ago I worked on an new project and I applied TDD once more, only this time I really enjoyed it! Just watch Ian's talk and you will get why. I couldn't have said it any better.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Never written a test in 20+ years of being a professional developer

Collapse
 
quii profile image
Chris James • Edited

dev.to/jonrandy/comment/92f8

Thought I'd seen this comment before.

For someone who hasn't written any tests you seem to want to make sure everyone knows you don't do it ;)

Collapse
 
lwriemen profile image
Lee Riemenschneider

These are also all good reasons for doing requirements analysis before coding. A benefit of TDD for people who don't like to document.

One of the problems with TDD is that you might code to assumptions and then not have tests that try to break your coding assumptions. YMMV

Collapse
 
bradtaniguchi profile image
Brad

I like the idea of writing tests for code I already know I'm going to test, but if I have no clue on how the code will look, I wont have any clue how to write the tests either.

Guess things come with experience :D

Collapse
 
jakenherman profile image
Jaken Herman

This is especially true in code-conversion work. You know what the intended logic is, so the tests can very easily be written before conversions are done. Good post πŸ‘

Collapse
 
herocod3r profile image
Jethro Daniel

Well for me, i write the implementation and write tests against it, i find this approach more effective, time and quality...

Collapse
 
jlouzado profile image
Joel Louzado • Edited

Agreed with how important it is to write tests first.

Just want to add, TDD and Test-First Development are similar but they have some subtle difference whose impact scales with the size of your project and team.

Ref: stackoverflow.com/a/48874859/3121906

Further reading:

Collapse
 
michi profile image
Michael Z

Was just watching Adam Wathan's TDD course twitter.com/adamwathan/status/1130...

Collapse
 
josegus profile image
Gustavo Vasquez

For everyone working with a team using laravel: ΒΏhow you write test? ΒΏHow you write a test first for a controller that doesn't already exists?

Collapse
 
sergei profile image
Sergei Sarkisian

TDD is great. But I have no idea how to do TDD, when your customer need the app/features to be ready yesterday, the requirements and business processes change frequently and code base is huge.