r/programming • u/mareek • Sep 18 '26
"the main purpose of unit tests is keeping your code from being violently sh*t on by others"
https://yosefk.com/blog/unit-tests-mark-territory-more-than-squash-bugs.html
742
Upvotes
531
u/Zenneth014 Sep 18 '26
Ah yes, the usual weirdly aggressive and antisocial superiority complex held by our favorite coworkers. You could just frame tests as “being more descriptive of the intention of the code” or you could frame it how the friendly author here framed it.
340
u/tanin47 Sep 18 '26
For me, tests don't even replace manual testing. It's mainly to prevent future regressions.
I don't think people are shitting on other people's code. High quality code changes can still miss some cases in other people's codebases.
14
u/BenchEmbarrassed7316 Sep 19 '26
It all comes down to contracts.
We have types that allow us to specify that a function accepts a restricted set of possible values of type T and returns a restricted set of possible values of type U (ignoring side effects and implicit dependencies).
However, in the absence of dependent types, we cannot specify within that contract that specific values of T will result in specific values of U.
Consequently, we have devised a cumbersome, unreliable, and poorly formalized method of describing these relationships using unit tests. Unit tests are "poor man's" dependent types.
28
u/zhivago Sep 18 '26
Or in other words, to prevent implicit contracts from being unknowingly broken.
46
u/pickle9977 Sep 18 '26
The main point of unit testing is cya because no one wants to spend the time to make sure they didn’t break shit before they slap some new bullshit feature in production.
This way we can all say who could have known, we’ll write a test for that.
Then one day we wake up and our ci build takes an infinite time testing the infinite bugs we create.
54
u/teerre Sep 18 '26
I don't that's quite so simple. Tests should guard against wrong changes in the future. The issue with tests, specially written by llms, is that they test irrelevant implementation details which makes correct changes more difficult. I recently started disabling tests when asking llm to write code because they are more noise than anything
The fact that someone didn't tests for
my.irrelevant.moduleis actually good because it denotes that that more module is irrelevant. It might be the case that they are wrong, but at that's much easier to find than what's important in code base with 5000 equally garbage tests18
u/Ghi102 Sep 19 '26 •
It's an overly cynical view from someone who has only seen bad unit tests.
And for the record, most programmers write bad unit tests. That's why LLMs are bad at it. That's because writing good unit tests is hard and most programmers don't spend enough effort ensuring they're actually writing good ones. Especially if you write them as an afterthought to hit some pointless metrics.
There's one point I agree with the author though, most codebases don't have enough tests aside from classical unit tests. Random tests, mutation testing and random property-based tests all can find significant issues ensuring the tests break for the right reason.
18
u/erebuxy Sep 18 '26
Nah, it is to make the shit solid and hard, so document all bugs and define them as features
15
u/GravyMcBiscuits Sep 19 '26 edited Sep 21 '26
95% of unit tests are a waste of time and resources. Testing should be based on the requirements ... Not the current implementation details.
The moment someone mentions code coverage metrics is the same moment you know they have missed the plot.
10
u/Solumin Sep 18 '26
I've only ever worked on infrastructure tests. I really, really want your project to have unit (and integration) tests, so that I can make sure my changes and upgrades don't break your project. So I broadly agree that the point of tests is to stop changes from breaking a project by making sure that invariants/assumptions/behaviors are correct and remain correct.
If I'm understanding the author correctly, they're a hardware developer more than a software developer, and that really shows.
The suggestion to use a randomizer is a good one, yes, and we have been using those for decades: fuzz testing and property-based testing.
There's no mention of test-driven development, where you write tests first and then implement the behaviors for them.
The focus on tests increasing a code base size is unusual; tests aren't free, necessarily, but generally more tests are seen as more good. My favorite projects in my career have had hundreds to thousands of tests.
"a culture evolves where you have no code ownership (“shared ownership” being the euphemism)" --- what? Are there companies out there where everyone is mucking around in everyone else's code? I mean, I could definitely see this in startups, but that's a totally different environment.
Integration tests should be written, but mocks are used because spinning up a couple dozen microservices for every CI run will cost you a lot of money and time.
The author skirts around the question of what makes a good unit test. It sounds like they encounter a lot of change detectors, the worst kind of unit test. But that doesn't mean unit tests are bad, but that they're hard to get perfect, just like everything else in software.
12
u/Glizzy_Cannon Sep 18 '26
Has the author just never written proper unit tests? I don't understand how you can boil unit tests down to "I add unit tests so that there's a false sense of security in my code and people won't shit on my code changes"
9
u/turkoid Sep 19 '26
27 em dashes in that post. Disregarding the egregious overuse, you can clearly see when they started to heavily use them — about 1.5 years ago.
3
u/Djamalfna Sep 20 '26
Used to be true. Now poorly-educated coworkers with Claude just crap all over the existing unit tests. Basically rewrite them from scratch any time they make a change. "Claude said that test was pointless" NO r whole point of the test was to make sure you didn't change critical architecture for something you refuse to understand gah!!
5
u/MantisShrimp05 Sep 19 '26
I see tests increasingly as the cement of an application. It knowingly makes changes to that code more costly because you usually have to also touch the test.
But when you have the core of the app and you don't want it to change this is great because it holds what is otherwise a very fluid thing in a certain shape even when we make changes or more specifically, makes the outcome of those changes more clear if it breaks the test.
But when a project or submodule is still new and the interface/approach has yet to be solidified, its not worth pouring cement into a not-committed design because it makes iteration harder.
Maybe with llms tdd is now both viable and preferable in many ways, but I still thing that old thought matters
4
u/riv3rtrip Sep 19 '26
Meh.
Unit tests are a way to flag regressions in contracts, and this matters when a lot of other people are working on code, or when code becomes extremely intricate and unwieldy.
Maybe that is what the author is saying, just way more irreverently. But, I don't know. They certainly don't seem to think of it in terms of contracts between components and across systems. The author frames "violently shat on" as change for the sake of it. The real thing you want to assert in unit tests is contracts across components and, more importantly, contracts for external consumers of the code.
The cynical framing doesn't work for me either. Organizations typically just have a large range of quality of contributors and that's just how the world is. Even if the quality floor is high within your organization, people can bring different opinions and beliefs to a code base. There's no reason to be cynical about unit tests or the idea of enshrining behaviors in stone.
2
u/ZukowskiHardware Sep 19 '26
I’m still shocked in my professional career at my coworkers reluctance to even think about unit tests. It is baffling to me.
2
u/BarryMcCoghener Sep 23 '26
99% of the bugs I've had would never have been caught by a unit test.
2
u/fagnerbrack Sep 24 '26
Defending code against your team members is next level teamwork... Around
Write tests to understand and lock specific cases of the SUT. If you don't trust your team members no Tech can defend it. Start solving the human problem first
1
u/mtutty Sep 19 '26
Others, including your idiotic future self. Hard agree.
1
u/Neither_Berry_100 Sep 19 '26
Yes and I don't just fine with mostly no testing. I have written tests for problematic code in the past. But without any frameworks. Not needed for smaller projects and non mission critical stuff.
1
u/the_ai_wizard Sep 19 '26
where "others" means next rounds with the LLM du jour
1
u/lizc-au Sep 22 '26
A agree whole-heartedly. Before unit tests etc were a thing - junior and (sometimes senior) programmers broke important things with regularity ... sometimes simple things and often code that had nothing to do with the change they were making ... leading to dire consequences like bringing an entire system down (I have personally been around when a colleague made an oopsie and it wasn't dealt with kindly back in those days). I personally sleep much sounder in my bed at night with tests in place especially my open-source code - even though I know full well a determined developer can break things deliberately - that at least I can check for without worrying about all the accidental stuff which I hope is covered off by the tests. Also getting AI to create comprehensive tests saves labor and has caught lots of my own errors.
1
u/NotMyRealNameObv Sep 23 '26
Joke's on you, I just re-write the tests as well.
1
u/dakkeh Oct 03 '26
Alternatively, bad code made to fit your unit test makes it impossible for your shitty code to be made well.
1
u/BigDukaDuka Oct 07 '26
You don't need UNIT tests for most projects.
There is no point in testing that 2 + 2 = 4, no one care what your function, module, unit, class, component or whatever the fuck is it is doing. It's not living in isolated world.
You do need INTEGRATION tests tho.
There is one small exception tho - you would like to have unit tests if you are writing some isolated library.
1
u/TransCapybara Sep 18 '26
In my testing, unit tests ensure the function signature contracts are correct.
1
u/edgmnt_net Sep 19 '26 edited Sep 19 '26
I think it boils down to unit tests doubling as a coping mechanism for lack of static safety, particularly type safety and memory safety in less safe languages. And they're also an easy metric to game or pretend you checked out.
Unit testing in this manner is pretty dangerous because it detracts from considering other, better ways of gaining assurance. And nobody really writes the good unit tests anyway, it's an entirely different matter to unit test something like a sorting algorithm
0
u/nicholashairs Sep 19 '26 edited Sep 20 '26
Bruh what 😂😂😂😂😂😂
Edit: for clarity I'm not agreeing with the author, I'm shocked and confused by what they seem to think
0
u/korevivel Sep 19 '26
tests save future you some pain
0
u/[deleted] Sep 18 '26
[deleted] — view deleted comment
0
u/[deleted] Sep 19 '26
[deleted] — view deleted comment