News for hackers. Stuff that matters.
Re:Unit Testing is Overrated (2020) by josefx on Thursday April 07, 2022 @07:20AM (#30943037)
> It pains me when I see developers testing their functionality end to end, manually

On the other hand I know developers that only unit test. Hundreds of lines of mock code, dozens of tests. No documentation, no sample configuration for the final application and dozens of data races because the unit tests don't cover multi threading. But hey an unusable buggy mess is a net positive as long as it makes the test coverage statistic happy and that is one of those things reported to management.

While a bit exaggerated one of the downsides of unit tests is that they quickly become part of a metric without regards for when they ad value.

Re:Unit Testing is Overrated (2020) by goto11 on Thursday April 07, 2022 @01:14PM (#30947473)
> Hundreds of lines of mock code

Mocking should be kept to a minimum and ideally avoided.

Mocking is most useful when adding tests to legacy code, but new code should be designed so it can be tested without the need for mocking.

Re:Unit Testing is Overrated (2020) by older on Thursday April 07, 2022 @05:01PM (#30950302)
Mocking own code should be avoided if possible, but it does not apply to external resources like database, http responses etc.
Re:Unit Testing is Overrated (2020) by simonw on Thursday April 07, 2022 @10:33PM (#30952721)
I don't think it's ever a good idea to mock a database. Invest in a test framework that can performantly spin up a fresh database instance and throw it away at the end of the test run instead.

Mocking outbound HTTP calls is worthwhile, but thankfully most testing frameworks I've used (at least in Python world) make that pretty easy.

Re:Unit Testing is Overrated (2020) by older on Friday April 08, 2022 @01:55AM (#30953754)
I mock database by using sqlite in-memory database. It is not 100% compatible with my production database but still god enough for most of my tests. This way I have fresh database for each unit test and the tests are still fast.
Re:Unit Testing is Overrated (2020) by adhesive_wombat on Thursday April 07, 2022 @04:19PM (#30949857)
Also can be pretty handy when there's hardware that needs something very specific to happen to work properly. Mocks can give you (some) confidence that when you poke the I2C bus, you won't hang it up.

But it's very easy to overdo it and end up testing irrelevant implementation details instead.

Re:Unit Testing is Overrated (2020) by AstralStorm on Thursday April 07, 2022 @07:43AM (#30943195)
Hence why you need automated integration tests, where possible. Especially for regressions.

They tend to have much higher value than pure unit tests.

Re:Unit Testing is Overrated (2020) by rectang on Thursday April 07, 2022 @11:21AM (#30945633)
I agree that automated integration tests typically offer higher ROI than equivalent unit tests written with mocks, because mock code is so difficult to write and fragile.

I disagree with your assertion that integration tests offer higher value than unit tests in general. Don't judge all unit tests by mock code.

Re:Unit Testing is Overrated (2020) by larkost on Thursday April 07, 2022 @07:23PM (#30951532)
I will counter that integration tests tend to have a much bigger footprint: one test covers a lot more ground and so per line of code will catch more things.

But, the failures from integration tests are often a lot harder to read. Even half-good unit tests tend to show exactly where the error is, and make the fix cycle much shorter. It is also much harder to write exhaustive integration tests for all of the inputs that might happen.

I, as a long-term qa-ish person (I usually write testing systems) see real value in having a mixture of both system, integration (multiple systems), and unit tests, as well as end-to-end manual tests. Anything big enough needs all of these.

Re:Unit Testing is Overrated (2020) by oxff on Saturday April 09, 2022 @02:46AM (#30965755)
If errors from your integration tests are hard to understand, that just means you need to improve the debugging information emitted by your software such that your developer can debug it properly using it, no?
Re:Unit Testing is Overrated (2020) by cush on Thursday April 07, 2022 @10:34PM (#30952728)
This is the point. Both are tools. They're both needed.
Re:Unit Testing is Overrated (2020) by vsareto on Thursday April 07, 2022 @10:41AM (#30945022)
TBH mock code just sucks to write, no matter how good your unit tests are.
Re:Unit Testing is Overrated (2020) by saynay on Thursday April 07, 2022 @11:14AM (#30945528)
That is one of the big benefits I have found from writing code while planning to write unit tests for it (even if you don't write all of them). You will generally avoid patterns that will require a lot of mocks to test, which generally make that code easier to refactor and reason about. If you are able to make big chunks of your code into "pure" functions, they are now really easy to test.
Re:Unit Testing is Overrated (2020) by atx42 on Thursday April 07, 2022 @02:01PM (#30948131)
Also agree.

I've seen code so mocked it was dubious that it could be testing anything, accurately at least.

Re:Unit Testing is Overrated (2020) by rectang on Thursday April 07, 2022 @11:23AM (#30945663)
A classic approach is that if you have to make some complicated API call with a bunch of arguments, you can put all the logic wrangling the arguments into a pure helper function and unit test the helper.
Re:Unit Testing is Overrated (2020) by mring33621 on Thursday April 07, 2022 @11:34AM (#30945867)
Agree.

I use strategy pattern with Java functional interfaces to avoid using magic mocking frameworks. That way, my mocks are just trivial impls of the plugin parts.

Re:Unit Testing is Overrated (2020) by 8note on Thursday April 07, 2022 @08:19PM (#30951926)
Setting up sample data is the biggest pain for mocking in my experience. But it's true however you're testing.
Re:Unit Testing is Overrated (2020) by geysersam on Thursday April 07, 2022 @02:00PM (#30948103)
How do you accomplish that? I often end up in a situation where mocks are needed (like in the article).
Re:Unit Testing is Overrated (2020) by saynay on Thursday April 07, 2022 @03:37PM (#30949389)
Often times, you will have chunks of code that are either creating input for what would go into an interface/mock, or handling the output of it. You could pull those bits, even if they are relatively simple (maybe only a few lines, and only used in that one spot), and extract those out to a pure function to sidestep the mock.

Sometimes you can't really avoid it. In those cases, I would judge how much of the effort is testing your code, and how much is testing your ability to write mocks. Unless it is a really important functionality, or something complex enough that it would be easy to get wrong, I wouldn't bother with most tests that need extensive mocking. Instead handle those later with integration / e2e tests.

Re:Unit Testing is Overrated (2020) by jseban on Thursday April 07, 2022 @08:53AM (#30943777)
> and that is one of those things reported to management.

Yeah unit testing as I see it is just optimising for the success of middle management, at the expense of both the organisation's goals, and the developers.

You are making predictability the only focus, because that's the metric which middle management is evaluated by. At the expense of performance, end product quality, and work satisfaction. "It was done in exactly the excruciatingly slow and mind numbing boring way, and was exactly as shitty as designed" = success for management.

If you would try to increase performance, end product usability, and/or work satisfaction, you would have to put the predictability at risk, which means that the success of the middle manager will not be the highest priority at all cost anymore.

Re:Unit Testing is Overrated (2020) by deckard1 on Thursday April 07, 2022 @01:38PM (#30947807)
yep. I've worked at places with unit tests/e2e/and static typing. And I've worked at places with zero unit tests/no e2e/dynamic typing.

The push for more testing and static typing (to degree) was always managers. They trade predictability for momentum.

The reality in both situations is that bugs happen. I've not noticed a difference in frequency of "critical" bugs in either scenario. The only difference is that at the no unit test/no e2e places, getting your code into production was trivial. Which always meant when bugs happen, they are fixed much faster than the unit test-everything place. We're talking minutes vs. days here. Because at a risk-adverse business (that is, any place more than a few hundred people), you have multiple stakeholders that have to sign off if you want to go take a piss in the bathroom.

Re:Unit Testing is Overrated (2020) by larkost on Thursday April 07, 2022 @07:40PM (#30951641)
While I totally agree that process can go nuts and get in the way, I will disagree that testing always makes things slower. Once you are past the prototyping phase, and get into actual production phase (some projects never make this turn) some testing infrastructure can absolutely make thing faster. Rather than having to deal with all of the bugs that your fellow developers are just dumping into the main branch, they can help weed out some of the bugs before others have to deal with them.
Re:Unit Testing is Overrated (2020) by ncann on Thursday April 07, 2022 @11:23AM (#30945664)
Your comment is downvoted perhaps because it sounds so cynical, but after thinking about it I agree to a certain extent. Throughout my career I've noticed the biggest pushers for more unit testing are usually middle managers who love to see that coverage number goes up since it's an observable metric, but nuanced opinions like "more unit tests are not always good and can be detrimental" are generally not welcome.
Re:Unit Testing is Overrated (2020) by mpweiher on Thursday April 07, 2022 @01:33PM (#30947736)
> Hundreds of lines of mock code

No wonder they are not getting value out of unit tests.

https://blog.metaobject.com/2014/05/why-i-don-mock.html