It truly makes sense for a definitive guide to deep dive into what the benefits of unit testing would be. From a developer perspective:
Give insight as to what unit testing should accomplish, so that a developer, can intuitively decide when appropriate to write a unit test.
Give confidence to developer as to why they should write unit test.
From a management perspective, to help introduce unit testing, for those that might be less inclined towards unit testing.
What is Unit Testing?
A unit in a best case scenario, is a function that always gives you the same result for a given input(pure function) Testing that unit, is to make sure that the expected result happens when running that function.
Benefits of Unit Testing
Refactoring. Change code once, and see everything else is working.
Focus(See TDD item #2)
Helps understand design of code working with(See TDD item #3)
Instant visual feedback that code works as expected.
Documentation (See TDD #4)
Helps with code-reuse. Ability to re-use code and tests. Tweak tests accordingly.
Testable code
Modular
Maintainable
Readable
What is TDD (Test Driven Development)
Start by writing a test.
Run the test, and any other tests. At this point, your newly added test should fail. If it doesn't fail here, it might not be testing the right thing, and has a bug in it.
Write the minimum amount of code required to make the test pass.
Run the test to check the new test passes.
Optionally re-factor your code.
Repeat from 1.
What is BDD?
Typcially when unit testing, a particular function at a later date can change its implementation. For instance, if we have a counter function, the counter can be changed to start at 5 instead of 0, breaking the expect statement of 1.
In BDD we focus on the intended behavior, instead of the expected result. The following is a great explanation:
The Benefits of TDD
Higher Test Coverage
Focus
Focus one part of an issue at a time.
Allows one to realize when to stop coding.
Interfaces. Allows you to think more organically about what should be put into interface. Allows for interface to be written bottom up (implementation, behavior) instead of top down (behavior, implementation).
What is BDD - Code Example
// Non BDDdescribe('Counter', () => {it('should increase count to 1', () => {const counter = new Counter();
counter.tick();expect(counter.count).toEqual(1);});})// BDDdescribe('Counter', () => {it('should increase count by 1 after calling tick', () => {const counter = new Counter();const expectedCount = counter.count + 1;
counter.tick();expect(counter.count).toEqual(expectedCount);});})
The Benefits of BDD?
If at a later time the counter(as seen above) has to change based on requirements(starting at 5, instead of 1), it will not affect the unit test.
What, When, and How
Unit testing is the what, TDD is the when, and BDD is the how.