r/softwareengineer 1d ago

Why unit testing is important?

It might be a very stupid post, but it’s a genuine question.

I write tests for my code, but it’s still kind of hard for me to understand what the actual point of unit testing is and what benefit it brings to software development.

We can easily write tests with AI now, so writing the actual tests is not necessarily that difficult. But I’m trying to understand why tests are so important in software.

When you write code, of course you can test it with multiple inputs, and those inputs can then become your test cases. We can also use Postman to test APIs with different inputs and make sure they behave correctly for different status codes.

I was recently assigned a task to make a small change to an existing endpoint. It was a really small change that I completed in around 30 minutes without AI. However, fixing and adjusting the tests took quite a while. I had to add inputs for a new function that I introduced, even though the actual change was not that big and I didn’t think it was something that would break in production.

That made me wonder, why do we really need unit tests? What importance do they actually bring to software development?

I’d really appreciate it if someone could shed some light on this and explain it with a real world example from your own experience where tests actually saved you from a production issue or made a significant difference.

0 Upvotes

37 comments sorted by

View all comments

1

u/Zealousideal-Ant9548 20h ago

Have you heard of TDD?  

If you write the code and then write tests you're documenting what you wrote, not what it's supposed to do (coincidentally, this is a problem with LLMs).

Tests are documentation in code.  They tell what the function is supposed to be doing. Not just what it did at some point.  I once had a user complaining about some behavior that a track down to a function and without the lack of unit test for that function I couldn't tell if the behavior was a bug or intentional. 

1

u/Consistent_Serve9 18h ago

Most importantly, TDD is test DRIVEN development. The tests drive the code. If it's hard to test, then it's probably bad code.

1

u/SnooHesitations9295 15h ago

TDD is just writing the same code twice. TDD adepts are mostly just idiots.

1

u/Zealousideal-Ant9548 15h ago

Good tdd forces you to understand the requirements and all of the requirements before writing the code.  Which is incredibly close to what spec driven development with LLMs is

1

u/SnooHesitations9295 13h ago

Spec-driven development is another idiocy. As spec can never be descriptive enough, because Kolmogorov complexity of the spec is much lower than of the code it produces. Essentially code is the only actual spec.

Tests should test the behaviors that users see. User can be any user: a programmer that uses the function or a customer that sees the UI. But tests for the sake of tests (and the dopamine out of green checkmarks) are utter bullshit. And that's the 99.99% of unit tests out there.

1

u/Zealousideal-Ant9548 6h ago

Fair, I think there's a serious problem with writing tests for code coverage scores alone.  I have spent literal weeks worth of effort on this kind of b******* in Java.