Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to Unit Testing | Introduction to Automation Testing
Introduction to Automation Testing
course content

Contenido del Curso

Introduction to Automation Testing

Introduction to Unit Testing

Unit Test is a type of test case designed to verify the functionality of a specific unit within the code. A unit typically refers to the smallest testable part of an application, such as a function, method, class, or module. The goal of a unit test is to ensure that this component behaves as expected in isolation from the rest of the program.

In this chapter, we will learn how to write some very basic unit tests for our application. Though a formal introduction to Unit Testing will be done in the next section - this chapter aims to explain the essence of Unit Testing through JavaScript using our own custom testing function rather than utilizing a testing framework.

It is important to note that in Unit Testing, we aim to test the Unit for as many cases or inputs as possible.It is important to include all the possible edge cases when writing unit tests. Knowing the code of a unit makes it easier to find potential edge cases.

Edge Case: An edge case refers to scenarios or inputs that lie at the extreme ends of the input space. These conditions or inputs are not necessarily unusual but are at the boundaries of what the function is expected to handle.

Input Space: The input space refers to the complete set of all possible inputs that a function can accept.

To understand edge case scenarios let's consider a function called calculateAverage:

html

index

css

index

js

index

copy

Following are examples of some edge cases that explore some unique situations for the above function:

  1. Passing an Empty Array Input: [] Expected Output: null
  2. Passing a Single Element Array Input: [42] Expected Output: 42
  3. Passing an Array containing Large Numbers Input: [1e10, 2e10, 3e10] Expected Output: 2e10
  4. Passing an Array containing Negative Numbers Input: [-1, -2, -3, -4, -5] Expected Output: -3

¿Todo estuvo claro?

Sección 1. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt