2 ms·
I think it depends a lot on the nature of the code. I prefer to use unit tests to probe tricky parts of the code. It's basically like a recyclable debugging se
by marginalia_nu 4y ago
I think it depends a lot on the nature of the code.
I prefer to use unit tests to probe tricky parts of the code. It's basically like a recyclable debugging session. Sometimes with an actual debugger attached, but otherwise just an assertion is fine. If I've had reason to investigate the code once there's probably a reason to keep that inspection hatch because bugs rarely come alone.
Unit tests for the sake of unit tests is kinda stupid though and can actually border on a sort of technical debt. If no matter what you change, you need to fix/disable a bunch of unit tests, failing tests becomes expected and it becomes very easy to fall into just fixing the assertions or disabling the test when that happens without actually thinking about why it fails. Not even mentioning how much this sort of code base will bog down the rate of change.
That said, it's a tricky balance. You can definitely have both too many and too few unit tests.
- MoreQARespect 4y agoIt does depend on the nature of the code. My rule of thumb is integration code -> integration tests work better. Algorithmic/calculation code -> unit tests work better. A debugger attached to an end to end test probably better if you're testing integration code. With a unit test you might have to do a lot more extra work to, e.g. construct an exact replica of a request object or a database object or something else that comes "for free" when you precisely mimic the software being used in the real world. That said, if mimicking the real world in your code is as simple as 'assert my_function("some input")' and checking the output then yea, unit tests are way quicker.