8 pointsby theturtlemoves8 hours ago7 comments
  • apothegm2 hours ago
    Unit tests are super useful when first writing a function or class to confirm it does what you think it does.

    Then throw away the unit tests and write integration or E2E tests instead. Then you can refactor under the hood while ensuring overall system behavior is as expected.

    There are some exceptions where you might want to hang on to a small subset of unit tests. They can be useful for demonstrating how to use an interface or class. They can help support particularly complex bits of logic. If a certain part of your codebase is fragile and regression prone, unit test coverage can help.

    Otherwise, they just calcify the code.

  • jackfranklyn6 hours ago
    My rule of thumb: unit test logic, integration test plumbing.

    If a function does actual computation or has branching logic that could go wrong in subtle ways, unit test it. If it's mostly wiring things together (fetch data, transform, save), an integration test catches more real bugs.

    The "unit test everything" era came from a time when spinning up test databases was painful. Now with Docker and in-memory DBs, integration tests are often faster to write AND more useful.

    Where unit tests still win: algorithmic code, parsing, anything with lots of edge cases. Writing unit tests forces you to think about boundaries. Integration tests just tell you "it worked this time."

    The worst outcome is having tests that make refactoring painful but don't catch real bugs. I've seen codebases where every internal method signature change breaks 50 unit tests that were essentially testing implementation details.

  • commandersakian hour ago
    My opinion is unit tests where you can easily craft inputs and check outputs.

    If that becomes hard or you find yourself mocking a lot, then stop, and instead write integration and e2e tests.

    Remember unit tests get tightly coupled to your code base, so use them wisely.

  • rvz7 hours ago
    The moment the software is in production, making a lot of money and is stable, I then add lots of tests (both unit and integration) to prevent a $1,000 issue turning into a $100,000 problem later down the line.

    Instead of testing everything to being perfect, 100% test coverage and never releasing and the company questioning why the deadlines were missed for the project because of testing dogma.

    > I've personally found that when the architecture of the system is not mature yet, unit tests can get in the way. Terribly so. Integration tests or system tests to assert behavior seem the starting point in this and other scenario's, including when there are no tests at all yet.

    Totally agree.

  • aurareturn6 hours ago
    I don't write any unit tests. Instead, I only do integration/system tests.

    At the end of the day, I need to know that the system works and does what it is suppose to do. Unit tests adds too much complexity in my opinion and isn't worth it.

  • hahahahhaah4 hours ago
    It is a matter of horses for courses. Be driven by what the tests cost and what they accomplish.

    Costs may be:

    * Developer time to make and maintain.

    * CI time means slower CI and higher costs there.

    * Ossification of source code (especially unit tests, less so integration). Meaning harder to refactor as you also need to rewrite tests.

    Benefits:

    * Finds bugs

    * Can be a handy local dev loop and local debugging loop

    * Documents code and proves that documentation is correct

    * Helps with AI assistance

    * Integration tests should make refactoring easier or more confident.

    I would err on the side of good coverage (80% excl stupid stuff) unless I have in hand a specific reasin not to.

  • bjourne4 hours ago
    The most important ones are those that catch bugs. If your system crashes you write a test that fails until you fix the bug. Don't punt on it because "it's hard to test" (code smell). Tests that never fail are of miniscule value.