My developer story: Unit tests

Liem Le
3 min readMay 12, 2019

Do you play a developer role in a tech company? Have you ever write a unit test for your code? Did you ever wonder if writing unit tests is necessary?

To begin with, I’m working as Java developer for a tech company in Vietnam about 4 years and I’ve just started to writing unit tests about 2 months ago.

Did it too late for me to learn about unit testing? No!

You know? I do test my a code in some “odd” ways before like: create main method for each class, debug in my head while writing code, print debug message to the console, etc… but not unit test.

Here is some excuses why I didn’t write test before.

The first excuse: “I need to release on time”

To be honest, this is the only excuse that I accept as time pass.

There was time that we can skip writing test and release ASAP, the time you do MVP (Most Viable Product).

After that, you should be careful and think hard about not writing test. Because at first, there was not much features so you can skip testing, but as business growth many features was added and “release on time” can not be reach cause of broken features.

The second excuse: “Test is not necessary and waste of time”

It’s not usually you would heard this excuse and I disagree with this one. As I have mention above, I (or we) do write test in some “odd” ways.

Let me tell you why write test could be save your time. Example: my subordinate may have to spend about an hour to implement one feature and another hour to test. So he need 2 hours to complete task with test, double huh?

Nope, as our application growth, he skip that 1 hour of test then my production service broken and I will have to spent an hour to find out what cause my service not working property and then tell him to fix the problem. The total time would be: 1 hour of implement + 1 hour of debug + 1 hour of write test + many hours in the future if there’s no test.

Is test waste of time?

No tests no party!

The third excuse: “Good developers doesn’t need to write test because their code is good enough” or “Elite developers write code that can run perfectly without checking.”

Your boss can tell you this, I can tell you this, but when problem occurred: “It’s your fault!” Believe or not, I let you to make your choice.

How did I survive without writing any test for years?

“I write testable code!”

“What is testable code?”

IMHO, testable code is code that satisfied SOLID principles, code that you can extend, mocking without changing any code structure. I will not talk much about SOLID, you can read more about SOLID principles here and here.

Why and when did I write unit tests?

I started to thinking about unit test when I got too much similar answers for the same, simple question: “Did this commit/change would break others code?”

And the answers are “Yes!”, “May be!”, “Not sure!”, “I dunno!”.

That was the moment I know need to write test.

You need to write test when you started to lose confident to change

Conclusion

Writing unit tests should be a culture more than just a requirement

Developers should learn to write testable code first, then write test and vice versa, learn to write test to write testable code (believe me, TDD is a good way to learn for new developers).

--

--