Benefits of Unit Testing

TDD + BDD - Angular: The Full Gamut Edition

Charlie Greenman
September 13, 2020
3 min read

Why Writing This

From a developer perspective:

  1. Give insight as to what unit testing should accomplish, so that a developer can intuitively decide when it is appropriate to write a unit test.
  2. Give confidence to developer as to why they should write unit test.

From a Management Perspective:

  1. To help introduce unit testing, for those that might be less inclined towards unit testing.
// BDD 
describe('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);
  });
});

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

  1. Refactoring. Change code once, and see everything else is working.
  2. Focus(See TDD item #2)
  3. Helps understand design of code working with(See TDD item #3)
  4. Instant visual feedback that code works as expected.
  5. Documentation (See TDD #4)
  6. Helps with code-reuse. Ability to re-use code and tests. Tweak tests accordingly.
  7. Testable code(modular, maintainable, and readable)

What is TDD (Test Driven Development)

  1. Start by writing a test.
  2. 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.
  3. Write the minimum amount of code required to make the test pass.
  4. Run the test to check the new test passes.
  5. Optionally re-factor your code.
  6. Repeat step #1.

What is BDD (Behavior Driven Development)

Typically when unit testing, a particular function at a later date can change it’s 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 Benefits of TDD

  1. Higher Test Coverage
  2. Focus
  3. Focus one part of an issue at a time.
  4. Allows one to realise when to stop coding.
  5. 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 BDD
describe('Counter', () => {
  it('should increase count to 1', () => {
    const counter = new Counter();
    counter.tick();
    expect(counter.count).toEqual(1);
  });
})
// BDD
describe('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);
  });
})

Benefits of BDD

If at a later time, the counter(as seen above), for instance, 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.

Convincing the skeptic might be difficult. The following are some solid arguments on why one should unit test:

  1. Too Simple to Fail? If this is used regularly in our application, doesn’t it make sense to spend 5–30 minutes testing it?
  2. Too difficult to Unit Test, won’t it be time consuming? Good indication that it needs to be re-factored, proving point of benefit of unit test.
  3. Too Time Consuming even with re-factored code?
  4. App will produce more bugs
  5. Lower Quality Code
  6. Lack of confidence. Perhaps bugs under the radar.
  7. Lower design implementation of code.

Thank you for reading, and keep being amazing you :)

More articles similar to this

footer

Razroo is committed towards contributing to open source. Take the pledge towards open source by tweeting, #itaketherazroopledge to @_Razroo on twitter. One of our associates will get back to you and set you up with an open source project to work on.