12
theSith
2y

When I was a junior engineer I used to hate writing unit tests but now I look forward to writing them.

Well written Unit tests will save your life

Comments
  • 1
    I've never worked an a large scale program or met a team that used unit tests...

    Like you write some code, then write a whole test where you compare the outcome to something that is true.

    public int Add(int x, int y)

    {

    return x + y;

    }

    unit test :

    int result = Add(5, 6);

    if (result != 11)

    throw new InvalidOperationException();

    Like how and why do you need that ? If you debug and see that it is not correct, isn't that faster ?

    Or do I miss the point of unit testing ?
  • 2
    @Grumm
    add(x, y) is one of the worst and over-simplified examples for unit testing
    It's useful where changing code affects another part or the behaviour (which you might not immediately notice)
    Saves time and isn't as error-prone as manual debugging
  • 1
    @devRancid and that is maybe why I never used it xD

    Thanks for the details.

    But most of my functions are doing very little. So using a profiler and debugger has always done the trick.

    And most of the stuff I work on are visual (creating drawings and configurators) so if something is wrong, I have instant feedback.
  • 3
    @Grumm

    1)
    Unit tests are great for cases where you have a wide range of inputs and edge cases that would be too extensive to check manually

    2)
    Unit tests are great to make sure your code not only does the right thing now, but also will remain doing the right thing in the future.

    3)
    Unit tests are great to explicitly document expected behavior via code that would otherwise be just lost or (slightly better) need large, ugly, ambiguous comment in code.
  • 0
    @Grumm do you have dependencies and update them? Do you never move stuff around, change the implementation?

    I've been working for a whole week trying to refactor something that results in serious cost saving. There where no tests for it. The changes where simple but the impact of a mistake rather large. Manual testing was tedious and undoable for repeated migration test datasets. If there where tests setup already I would have been done in a day.
  • 0
    @hjk101 Thanks for the info.

    No I don't have dependencies.

    So I probably never have worked on a project that big. I refactor a lot of stuff lately, but again, if I change something, and I run it and there are no errors + I see the correct result on screen then I know the changes works.

    Like I said previously, most of the stuff I work on, either work (and I have a visual feedback) or I get an error message and know what/where to fix it.

    (It looks simple and nice, but I am still talking about a 20k+ lines code base :P divided in large chunks)
Add Comment