Not writing unit tests just isnât an option for a reliable app in the long term. But, itâll take way more than 10 minutes, always.
Good tests remove development time, not add to it.
I canât tell you the countless hours saved from automated testing. Red Green refactor for life!
One rule of thumb Iâve heard and follow is that every time you encounter a bug, you write a unit test that would catch it. I find that does a pretty good job of getting high code coverage, though maybe thatâs cause my code is naturally buggy đ .
- Writes test to trigger the bug
- Test is green without fixing the bug.
- Dafuq?
Not testing is crazy. Once you realize you can actually refactor without ever having the fear youâve broken something, thereâs actually opportunity to make rapid improvments in structure and performance. Taking 2 minutes to write the test can save your hours of debugging. Unless youâre building a throwaway prototype, not unit testing is always the wrong choice.
Hereâs my take. In order to be able to write meaningful unit tests the code should be structured in a certain way, with very modular, decoupled units, dependency injection, favoring composition and polymorphism over inheritance and so on.
If you manage to write your code this way it will be an objective advantage that will benefit the project even if you donât write a single unit test. But it does make unit tests much easier to write, so presumably youâll end up with more tests than otherwise.
IMO teams should prioritize this way of writing code over high test coverage of non-modular code. Unit tests for deeply-coupled code are a nightmare to write and maintain and are usually mostly meaningless too.
I really hate the projects I work on where theyâve overtested to the point of meaninglessness. Like every single class has a full suite of tests mocking every single dependency and itâs impossible to look at it without breaking 50 test cases.
Similarly I hate the projects with no tests because I canât change anything and be sure Iâve not broken some use-case I didnât think about.
Much prefer the middle ground with modular, loosely coupled code and just enough tests to cover all the use cases without needing to mock every single little thing. It should be possible to refactor without breaking tests.
This. Just thinking about how you would test something leads to better code, at least in my experience.
When you can press âGoâ and dozens of little green lights light up? Thatâs the stuff.
Removed by mod
All you folks are crazy not to unit test personal projects. Unit tests donât need to be fancy and exhaustive. A sanity check and having a simple way to execute isolated code is well worth the 15 minutes of setting it up. Heck, just use them as scratch files to try out libraries and APIs. I canât imagine having the kind of time to raw-dog that f12 button and sifting through print() nonsense all night.
I think it also very much depends on your tooling & how easy it feels to start writing unit tests.
When I work in a Java project for example I always write unit tests even for personal stuff, because the IDE integration is great and itâs really quick to create a test class, run it and see granular results. I donât feel the same way about testing JavaScript because the tooling at least for me hasnât worked quite as well (though that could very well be my own fault, itâs been a while since I looked into it). The more cumbersome setting up and running the tests is, the more tempting it becomes to just use the console or manually test parts instead.
If I ever want to take a break from my personal project and come back to it. Unit tests are necessary. If I try to continue working on a project that doesnât have unit tests. I feel like every bit of code I touch is introducing countless hours of debugging. It really demoralizes me.
Thatâs why you write the tests first
TDD is pretty difficult when you donât have a very clear idea of whatâs the best way to achieve what you want to do â which is most projects most of the time.
Itâs usually also a hard sell for management, especially if the project is on its first steps and it has to show something functional in order to keep its budget.
Last but not least, the problem domain for unit tests is pretty focused. The parts of the code that are suited for unit testing may not be all that much. I know that TDD is not just unit tests but I mentioned it in the context of this discussion.
1st problem - you should be coding such that you can make at least two iterations. It should be perfectly okay to code it once so that you have an idea and then throw it out and do it correctly. If you work in sufficiently small steps, thereâs barely any time waste plus you save time that you would spend maintaining
2nd problem - Kent Beckâs (the guy who âinventedâ TDD) idea behind TDD was that it should help you be less stressed. If your job is to make a prototype or get something really out the door really fast and you wonât lose sleep over it, then there is no need to write tests. TDD is there to enable you.
I am not exactly sure, what you mean with the unit tests, so apologies if I am misunderstanding. I use integration tests in my red-green-refactor loop as well
Budget+Client expectations donât always give you time for two iterations
I hate manually verifying stuff. Thereâs no way Iâm going to walk through all the cases I care about after making a change. So I just run my stupid tests.
Testing is so important though! It can be mind-numbingly boring, but it is an important part of software development, imo.
100%!
For personal projects? Sure. At work? MR doesnât get approved if the coverage drops even by 0.02% :(
this especially makes you feel idiotic when you spend a long ass time finding & fixing a simple bug resulting from a typo then realize that it wouldâve been caught immediately if you just wrote tests like you said you would 3 days ago
whoah okay no need for personal attacks
youâll change your mind when itâs more effort to set up a test situation for a manual test. like when you change something that communicates with an expensive propietary API and you canât just push it to production.
This is so painfully true it hurts. Especially for personal projects.
i have never written a unit test for a personal project in my life itâs not worth the overhead imo
I prefer acceptance tests or end-to-end tests mostly for personal projects. Libraries I write will have unit tests though.
However, for the most part. Personal projects are a lot of prototyping and throwing away. Unit testing and testing in general can get in the way of that.
Copilot Chat can write some pretty good unit tests now
A good unit test is an expression of the intent of the programmer that wrote the unit of code. How would Copilot know what your intent was?
What Copilot does is to test exactly what the code is currently doing⌠which kinda defeats the purpose.
But it does raise a good point, because thatâs usually exactly what teams are asked to do (especially when thereâs a high coverage requirement). Itâs completely pointless busywork, but thatâs what makes it perfect for Copilot.
Copilot Chat uses GPT-4 and can see more context than just the line youâre on. It has more reasoning than the autocomplete model.
Copilot happily writes failing testa too, TDD style. It just generates code tests that are very common and probably wonât catch those hard to miss cases anyway.










