Hacker News new | past | comments | ask | show | jobs | submit login
In Defence of Unit Tests
2 points by davedx 11 months ago | hide | past | favorite | 2 comments
It's popular in tech circles to dunk on unit tests, with the common theory being "end-to-end or integration tests are better" because "they test the system as a whole".

While I agree there is value to integration and end-to-end tests, I think a lot of people are missing the forest for the trees.

Ensuring your user interface doesn't show embarrassing errors when it interacts in some way with your back-end services is a good thing, don't get me wrong. It will help you to deliver software that doesn't occasionally prevent users from doing what they need to do.

But skipping unit tests is worse than skipping higher level tests. Why?

Because only unit tests are ever going to make sure your system behaves correctly.

If you're working on an e-commerce website that needs to calculate tax on orders, you will, practically speaking, never cover all the different cases with end-to-end or integration tests.

We're talking about ensuring the core business logic - the most important part of the system, the part you really don't want to get wrong - functions correctly in all the cases it's supposed to function. Get that wrong and instead of a user getting an error message, they might end up paying the wrong amount for a product. Have fun sorting that mess out when it impacts thousands of orders! (Source: I've worked on high traffic e-commerce systems broken like this).

Please don't mistake me: aiming for some arbitrary unit test "coverage target" is dumb. Your engineers should be responsible and write unit tests to cover important code. They probably should not write unit tests to cover some endless sprawling front-end react application's components. That's just wasteful. (Suggestion: don't build UI component libraries from scratch).

But developing software and skipping unit tests because "integration tests are better"? [1] Dangerous, reckless and bad. Don't do this!

[1] (This is not a straw man, I've seen this argument made time and time again).




> It's popular in tech circles to dunk on unit tests,

Tbh this is news to me. I've never experienced dunking on unit tests to be any more popular than dunking on anything else in tech, including integration tests. Everything gets dunked on by someone, and after some circular dunking the consensus seems to usually end up being "We should all just use the right tool for the right job".


I find it unhelpful that you open with a reference to the counter argument rather than simply stating what is so valuable about unit tests.

I think it indicates, to some degree, the veracity of the counter argument.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: