• lemmyvore ( lemmyvore@feddit.nl ) 
    link
    fedilink
    English
    arrow-up
    17
    ·
    edit-2
    3 years ago

    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.

    • Buckshot ( Buckshot@programming.dev ) 
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 years ago

      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.

      • TehPers ( TehPers@beehaw.org ) 
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 years ago

        My favorite tests are the tests that check what the function in question does when it’s passed null/wrong type/whatever in a statically typed language.

        The compiler’s there to make sure you don’t do that. Unless it’s something that can actually happen, I don’t think there’s much value writing dozens of these tests for each source file. By all means, test edge cases, but if the edge case violates the contract of the function being tested then I think it’s safe to say the caller is the problem, not the implementation.