DEV Community

Discussion on: Writing Good Unit Tests: A Step By Step Tutorial

bennyflint profile image
Benjamin Flint

This is a really good tutorial, but there was an early line that almost made me stop reading. If you were to change, "Let's imagine we just wrote a method...," to "Let's imagine we're about to write a method...," and adjusted the content accordingly, then you could change the title to "Writing Great Unit Tests" instead of just good ones.

ice_lenor profile image
Elena Author

Hi Benjamin,
I think you're hinting at TDD.

In real life, you usually solve a problem, not simply write a piece of code. Unlless you have an amazingly detailed spec, the interface is usually unknown, and you cannot write tests for it at this stage. Usually, it takes some time before the interface is finalised. Thus, tests have to be postponed for a bit.

Second reason I didn't mention it here is because following TDD is rather difficult, especially for beginners. I don't want to scare developers off unit testing because they would think it is too difficult. It is actually easy - if you are starting slowly, with simple ways to do it.

Thirdly, I don't mind which way you come to the final result. If the final result is code sufficiently (sic!) covered with tests, I don't mind if you wrote tests before or during or after. One absolutely can write a great unit test suite without following TDD religiously. Gatekeeping of this sort - "you're not a good developer if you don't follow TDD" - has never seemed a good idea to me.

I usually advise to write tests along with the code, if possible, but I don't make it mandatory.

I don't mind, of course, if people do follow TDD. If it works for you, great! If you prefer it, why don't you write an article explaining its benefits?