The TDD That Works for Me
The subject of test-driven development seems to be pretty hot these days. Of course, I have my own take on it; let me share.
Join the DZone community and get the full member experience.Join For Free
test-driven development (tdd) was rediscovered by kent beck and explained in his famous book in 2002. in 2014, david heinemeier hansson (the creator of ruby on rails) said that tdd is dead and only harms architecture . robert martin (the inventor of the solid principles ) disagreed and explained that tdd may not work only in certain cases. a few days later, he even compared the importance of tdd with the importance of hand-washing for medicine, and added that "it would not surprise me if, one day, tdd had the force of law behind it." two years later, now just a few months ago, he wrote more about it, and more , and more .
this subject seems to be hot. of course, i have my own take on it; let me share.
la grande bellezza (2013) by paolo sorrentino
in theory, tdd means "writing tests first and code next." in practice, according to my experience with working with more than 250 developers over the last four years, it means writing tests when we're in a good mood and have nothing else to do. and this is only logical if we understand tdd literally, by the book .
i have never done tdd by the book — tests first and code next.
writing a test for a class without having that class in front of you is difficult. i would even say it's impossible if we are talking about real code, not calculator examples. it's also very inefficient because tests by definition are much more rigid than the code they validate — creating them first will cause many re-do cycles until the design is stabilized.
code, deploy, break, test, fix
even though i'm a huge fan of automated testing (unit or integration) and totally agree with uncle bob that those who don't write tests must be put in jail , i just have my own interpretation of tdd. this is how it looks:
- first, i write code without any tests. a lot of code. i implement the functionality and create the design. dozens of classes. of course, the build is automated, the deployment pipeline is configured, and i can test the product myself in a sandbox. i make sure " it works on my machine ."
- then, i deploy it to production. yes, it goes to my "users" without any tests because it works for me. they are either real users if it's something open source or one of my pet projects , or manual testers if it's a money project.
- then, they break it. they either test it or they use it; it doesn't matter. they just find problems and report bugs. as many as they can.
right after some bugs are reported, i pick the most critical of them and — voilà! — i create an automated
. the bug is a message to me that my tests are weak; i have to fix them first. a new test will prove that the code is broken. or maybe i fix an existing one. this is where i go "tests first." i don't touch the production code until i manage to break my build and prove the problem's existence with a new test. then, i do
finally, it's time to
the problem. i make changes to the production code in order to make sure the build is green again. then, i do
git push. and i go back to the "deploy" step; the updated product goes to my users.
once in a while, i have to make serious modifications to the product, like to introduce a new feature or perform a massive refactoring. in this case, i go back to the first step and do it without tests.
the reasoning behind
the justification behind this no-tests-upfront approach is simple: we don't need to test until it's broken, mostly because we understand that it's technically not possible to test everything or to fix all bugs. we have to fix only what's visible and intolerable by the business. if the business doesn't care or our users and testers don't see our bugs, we must not waste project resources on fixing them.
i only create tests later when my users express the need for them by reporting bugs.
on the other hand, when the business or our users and testers are complaining, we have to be very strict with ourselves; our testing system is weak and must be fixed first. we can't just fix the production code and deploy, because in this case, we may make this mistake again after some refactoring, and our tests won't catch it. the user will find the bug again, and the business will pay us again to fix it. that will be the waste of resources.
as you can see, it's all money-driven. first, don't fix anything if nobody pays for it. second, fix it once and for all if they actually paid. it's as simple as that.
thanks to this test-and-fix-only-when-broken approach, the balance between production code and test code is not the same over the entire project lifecycle. when the project starts, there are almost no tests. then, the number of tests grows together with the number of bugs. eventually, the situation stabilizes, and we can move the product from beta version to the first release.
i created a simple command line tool in order to demonstrate the statistics from a few projects of mine, to prove my point. take a look at these graphs.
yegor256/takes (web framework, java):
yegor256/xembly (xml builder, java):
jcabi/jcabi-aspects (aop library, java):
yegor256/s3auth (s3 gateway, java):
first commercial project:
second commercial project:
in each graph, there are two parts. the first one on the top demonstrates the dynamics of production (green line), test-related hoc (red line), and the number of issues reported to github (orange line).
the bottom part shows how big the test-related hoc portion is relative to all project activity. in other words, it shows how much effort the project invested into automated tests compared with the total effort.
this is what i want you to pay attention to. the shape of the curve is almost the same in every project. it looks very similar to a learning curve , where we start to learn fast and then slow down over time:
this perfectly illustrates what i just described above. i don't need tests at the beginning of the project; i create them later when my users express the need for them by reporting bugs. this dynamic looks only logical to me.
you can also analyze your project using my tool and see the graph. it would be interesting to learn what kind of curve you will get.
Published at DZone with permission of Yegor Bugayenko. See the original article here.
Opinions expressed by DZone contributors are their own.