this post was submitted on 05 Aug 2023
673 points (97.9% liked)

Programmer Humor

31217 readers
37 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 4 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 166 points 11 months ago (6 children)

TDD is great when you have a very narrow use case, for example an algorithm. Where you already know beforehand: If I throw A in B should come out. If I throw B in C should come out. If I throw Z in an error should be thrown. And so on.

For that it's awesome, which is mostly algorithms.

In real CRUD apps though? You have to write the actual implementation before the tests. Because in the tests you have to mock all the dependencies you used. Come up with fake test data. Mock functions from other classes you aren't currently testing and so on. You could try TDD for this, but then you probably spend ten times longer writing and re-writing tests :-/

After a while it boils down to: Small unit tests where they make sense. Then system wide integration tests for complex use-cases.

[–] [email protected] 31 points 11 months ago (1 children)

Totally agree.

I think we should all strive to do better. Unit tests, mock-ups, UX design, 2 week sprints with actual working deliverables, well documented use cases, every thing neatly stacked in Jira, dev,test,staging,prod environments, continuous integration and every thing else we are told to do.

Then reality sets in……

With all that said, 25 years as a dev, this utopian environment is almost impossible to find unless forced by regulatory compliance. Medical devices, life critical systems, etc. or if you have big piles of money.

[–] [email protected] 20 points 11 months ago (1 children)

In my experience, those things tend to be forced by project managers who believe the highest law of the land is proper scrum. Unsurprisingly, this makes all the devs miserable with no way to change anything because "this is just how it's done".

[–] [email protected] 1 points 11 months ago

Hahaha. Yea. Been there too.

[–] [email protected] 24 points 11 months ago (1 children)

then you probably spend ten times longer writing and re-writing tests

This is always what I've seen personally when people use TDD. And it's worse because the inevitable time crunch towards the end of the project means the developers stop maintaining the tests, which renders all of the work put into the tests up to that point useless.

[–] [email protected] 12 points 11 months ago

That's not a problem with unit tests, that's a problem with project management

[–] [email protected] 13 points 11 months ago (1 children)

It's also great for bug fixes. Write that sucker first and you have an easy way to reproduce the issue and check whether it's fixed.

[–] [email protected] 9 points 11 months ago

Yeah, I'm constantly recommending junior devs to use TDD specifically for this. I don't recommend it for anything else. If they don't write the test first, it's possible that the test will end up testing the wrong thing and thus they can't be sure they really did fix the bug.

Sometimes it's hard to tell where to write the test ahead of time, so sometimes a slight variation I do is to write the test after (usually because it was such a struggle to figure out where the bug is), but when I'm testing it, I'll comment out the fix or whatever and make sure the test fails.

[–] [email protected] 8 points 11 months ago
[–] [email protected] 3 points 11 months ago

I mean, it sounds more like "The messier your project, the more difficult the unit testing". What you're describing sounds like issues with SRP and LoD. Which will inevitably happen as big projects get rushed, but let's place the blame where it belongs: rushing.

Yes unit tests take longer up front, but for projects that you need to update and maintain for a long time, they're a huge boon.

You can't do everything with a unit test obviously.

[–] [email protected] 2 points 11 months ago

I always start with the basics for my test cases. Like, every test case has a name, and assert(true) in the body.