Quick Guide: Unit Testing

Prevention is better than cure

Sabarish
YavarTechWorks
4 min readJun 24, 2020

--

Target Audience: Developers who want to improve the quality of their output.

Prerequisite: Basic programming knowledge, No testing prerequisites.

Difficulty level: Beginner.

Agenda:

  • Intro
  • What is Unit testing?
  • Why?
  • How?
  • FAQ
  • Next Steps
  • Reference Links

Intro

The context of a Unit is the smallest testable part of any software. The context of a Testing is to validate that each unit of the software performs as designed. There are different types of software testing, out of the various types, the three major types are Unit testing, Integration testing, and E2E testing. Let us see the definition of three major types and dive deep into Unit testing.

1. Unit testing.

It is done against a single “Unit” of code. We are going to see more about Unit testing in this article.

2. Integration or functional testing.

It is done against more than the unit, but less than the complete application.

3. End to end testing.

It is done against a live running application. We automate the front end. We write tests, that manipulate the browser and check if the application works.

What is Unit testing?

Unit testing is a type of software testing, where an individual unit of software is tested. A unit is the smallest testable part of any software. The purpose is to validate that each unit of the software performs as designed. As a developer, we probably know more about Software Development Life Cycle (SDLC), but we probably not know more about Software Test Life Cycle (STLC). The first step of STLC begins with developer’s Unit Testing.

Why

Unit Testing is performed by software developers themselves so that major issues/bugs are found at their end and development can be faster. Cost of finding and fixing defects increases over time in SDLC. The bottom line is that if you’re a developer, you are ultimately responsible for the quality of the code you produce. That means you should be writing unit tests — regardless of organizational structure

How

As Unit testing is testing a single unit, what will happen if the unit has some dependency, do we test that dependency, if we test that it will no longer be a unit test, it will become an integration test. Enters ‘Mocking’.

Mock is instead of using a real dependency, we are going to provide a mock dependency. Mock is class, that looks like a real class, but we can control what it does. There are different types of mocks.

  • Dummies (Objects that fill place)
  • Stubs (Objects with controllable behaviour)
  • Spies (Objects which keep track of which methods were called and how many times)

FAQ

Question 1: What are the phases and activities involved in STLC?

Answer 1:

Next Steps:

In this article, we have seen about the theoretical part of Unit testing. In the next article, we will look into practical part in Angular.

--

--