News for hackers. Stuff that matters.
Re:Unit Testing is Overrated (2020) by jseban on Thursday April 07, 2022 @06:55AM (#30942900)
> Then I showed that a unit test can discover this

Yeah but the catch is that in order for your code to be "testable" you have to rewrite it completely in a way that comes with large sacrifices, making it much more confusing, abstract, larger and much harder to work with, in every way except testing it.

Re:Unit Testing is Overrated (2020) by rootlocus on Thursday April 07, 2022 @07:20AM (#30943044)
My experience is the exact opposite of this. Modular code that has well defined boundaries and responsibilities can be mocked and tested in isolation. It can also be reasoned about in isolation, reducing complexity. New functionality can be assigned to the proper module because each module has a well defined purpose.
Re:Unit Testing is Overrated (2020) by Deukhoofd on Thursday April 07, 2022 @07:12AM (#30942989)
I think the focus that everything should be testable is a wrong one, but almost every codebase has functionality that can be easily unit tested without major refactors. For everything else, integration tests can already help a lot.
Re:Unit Testing is Overrated (2020) by catlifeonmars on Thursday April 07, 2022 @11:09AM (#30945453)
Ideally, everything should be testable or replaceable. The non testable parts of your code have network effects on the testability of other parts of the code that are dependent on those parts. This is where dependency injection (e.g via a constructor or function parameters) really shines.

I totally agree that if you have to choose (and usually you do, unless you have unlimited time and funding), choose to implement end to end integration tests first.

Re:Unit Testing is Overrated (2020) by esics6A on Thursday April 07, 2022 @08:14AM (#30943420)
The simple verification aspect of unit tests makes writing code easier and more purposeful in my opinion. At a most basic level it's kind of like a small informal proof. When the code base get's complex and larger in size this is invaluable if unit tests are written correctly. Tests have to make sense and be written well just like any other piece of code.

It also helps form thinking about test cases and logic before writing code. Before I used exhaustive unit tests I would just start writing code right away and often times this would result in constant refactoring as the code evolved or during debugging. But unit tests encouraged me to scaffold out abstract functions and write tests before implementing the code. It got me thinking about what the logic and workflows then implementing the functions meant having the tests pass as verification.

Re:Unit Testing is Overrated (2020) by catlifeonmars on Thursday April 07, 2022 @11:04AM (#30945366)
So if you an existing, functional, code base, I totally agree. Rewrites in general are rarely worth doing. If you have a new code base, it can be incredibly valuable to design the code such that externalities can be injected. Usually this means being able to swap out file system, RNG, system time, and networking layers. If your code uses a particular hardware platform, it can be useful to put the platform specific code behind an abstraction layer.

Not only does it make your code more unit testable, it makes your code much more ad hoc testable. Again, I would not refactor a large, non-disposable application just to achieve this.

Re:Unit Testing is Overrated (2020) by aequitas on Thursday April 07, 2022 @07:19AM (#30943031)
Imho one of the benefits of unit testing is precisely that you have to write/refactor your code to make it more testable and in the process you uncover and fix flaws in lacking abstraction and isolation.
Re:Unit Testing is Overrated (2020) by ivanche on Friday April 08, 2022 @06:04AM (#30955209)
If you write code that is not testable, how do you test it? What you propose is false dichotomy really. Code that isn't testable is almost always confusing, large, hard to work with etc.