DEV Community

Discussion on: Are unit tests a waste of your time?

vinu profile image

I agree on the fact that there has to be UTs to cover anything that has medium level complexity or higher in the codebase. But what I have seen in the recent years is devs are writing what we used to call as "checksum" test cases. They write test cases that test, for example, a function x() {} calls functions a() and then b() and then c() and assigns so and so value to a variable. What they are essentially doing is writing a hascode protection for x(){}. In this scenario, when another dev wants to refactor x() they have to rewrite the entire test case. So the problem here is, unit tests written are not testing the output of x(), rather they are forcing x() to be written in a specific way. Tests should focus on what, not how. Thats where unfortunately many projects are failing the overall goal. Even in our current system (6 years old), we have 25+ angular apps in a mono-repo and a whopping 10,000+ UI unit tests. We continue to see considerable amount of defects. When we finally ran a stat on how effective the existing UTs are, we found they only prevented about 8% of defects during development. If I were a business owner and spending so much money on testing, I would expect that % to be much higher. On the other hand, we found a solid reliability on our E2Es and ITs. E2Es and ITs are harder to write, but they provided us the best ROI so far.

Thread Thread
rossdrew profile image
Ross • Edited on

Your example isn't clear but either it's...

A method which calls three others and assigns a value outside the unit which is just testing that value was set on a mock. The unit test is describing that the unit passed responsibility of assigning a value elsewhere. We just need to check the value is correct.

A method which calls three others and assigns an internal value which is a unit test that checks a unit has a certain value after x() is called. The unit test is describing that calling x() mutates the state of the unit.

A method which calls three others and returns a value which is a unit test to check the return value. The unit test is describing that a value is expected when this method is called.

In either of these you could run 1000 tests in a split second. You will never reach that kind of coverage on E2E and if you do it will cost you a hugely unreasonable time investment. All of these tests focus on aunit as a black box and therefore a What and not a How so not sure what your complaint on that front is.

It sounds like your E2Es are providing the best value because your units or your unit tests, likely both, are badly designed.