âWe donât have time to write unit testsâ or âWe donât have the budget for unit testingâ are complaints I hear very often. Sometimes it may sound like, âWe donât use TDD, so thatâs why there are no unit tests,â or even âTDD is too expensive for us now.â Iâm sure youâve heard this or even said it yourself. It doesnât make any sense to me. I donât get the logic. In my understanding, unit testing is not a product; itâs a tool. You use tests to develop a product faster and better. How can you say you donât have time to use the tool that makes your work faster? Let me show you how.
TDD or not, a unit test is a unit test. Either you create it before the main piece of code or after it.
A unit test is a tool that helps you, a developer of software, ârunâ your stuff and see how it works. How else can you check if it works? When I hear, âI donât have time for unit tests,â my next question is: âHow did you test your code?â
I seriously canât understand how it is possible to write something and then not test it. Well, unless youâre paid monthly and nobody really cares about your deliverables. If you do care about the software you produce, youâre interested in seeing it in action, right?
So, how do you do this?
If itâs a one-page PHP website, you can probably run it locally on Apache, modify it on disk, and then Cmd+R many times. That will work for a primitive piece of code and only for you, a single developer of it. But I hear this âI donât have timeâ argument from programmers working on enterprise systems. How do you guys test your code?
I would compare unit tests with OOP classes. You can design the entire application in a single class with a few thousand methods. You will save time on creating other classes, structuring them, thinking about connections between them, etc. It will be a single 20,000-line .java file. And youâll say that âyou didnât have time to create classes,â right? What would we say about such a product and the author of it? Right, weâd say he or she is just stupid. And it has nothing to do with time or budget. Such a programmer just doesnât know how to use object-oriented programming tools, like encapsulation, inheritance, polymorphism, interfaces, method overloading, etc. Itâs not about time or budget; itâs about skills and discipline.
The same is true for unit tests. If you create code without unit tests, it may work, just like that monster class with 20,000 lines, but the quality of your product will be very low. And not because you didnât have time to write unit tests, but because you didnât know how to do it.
So every time I hear, âI didnât have time for unit testing,â I understand that you just didnât know how and are trying to conceal that fact behind false excuses. Itâs not professional, to say the least.
Why don't you write unit tests? #testing
— Yegor Bugayenko (@yegor256) December 30, 2018
