DEV Community

Cover image for Unit test in programming
Em Ha Tuan
Em Ha Tuan

Posted on

Unit test in programming

What is that πŸ™‹?

In computer programming, unit testing is a software testing method by which individual units of source code-sets of one or more computer program modules together with associated control data, usage procedures, and operating proceduresβ€”are tested to determine whether they are fit for use.

Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure. - Wikipedia.

Why do we use it? I think it is making us spend more effort to do something which end-users never use ...

Unit test is useless for end-users

Yes, it is true. Unit testing may not be directly used by end-users πŸ¦Ήβ€β™‚οΈ. BUT it is still a critical part of the software development process. Here are some reasons why we use unit testing.

To ensure correctness

Unit testing helps to ensure that each component of a software system behaves as expected. By testing each unit of code in isolation. We can verify that is works correctly before integrating it with other parts of the system.

correctness in unit test

To catch bugs early

Unit testing help to catch bugs early in the development process, when they are less expensive to fix. By detecting issues as soon as they are introduced, we can avoid downstream problems that might be more difficult and time-consuming to solve.

To catch bugs early

To facilitate change

Unit testing helps to make it easier to change software over time. By have a suite of tests in place, developers can be confident that their changes do not break existing functionality.

To facilitate change

To improve code quality

Unit testing helps to improve the quality of software by encouraging good programming practices. By writing tests for each unit of code, developers are forced to think about the expected behavior of their code, which can lead to better-designed and more maintainable software.

To improve code quality

Fetching image function by Javascript

Here's an example of a simple function in JavaScript that fetches an image from https://picsum.photos/1080:

async function fetchImage() {
  const response = await fetch('https://picsum.photos/1080');
  const blob = await response.blob();
  const url = URL.createObjectURL(blob);
  return url;
}
Enter fullscreen mode Exit fullscreen mode

And here's an example of a unit test for this function using the Jest testing framework:

test('fetchImage returns a URL', async () => {
  const url = await fetchImage();
  expect(typeof url).toBe('string');
  expect(url).toMatch(/^blob:/);
});
Enter fullscreen mode Exit fullscreen mode

This test ensures that the fetchImage function returns a URL string that starts with blob: (since the response from the fetch API is a Blob object). If the test passes, it means that the function is working as expected and can be used in other parts of the code with confidence.

Summary

  1. Unit testing is a software testing technique.
  2. Unit tests are typically automated and use specialized testing frameworks or libraries, such as JUnit for Java or Jest for JavaScript.
  3. The goal of unit testing is to ensure that each unit of code is correct, reliable, and meets its design requirements.
  4. Unit testing is just one part of the software testing process, and it should be used in conjunction with other testing techniques, such as integration testing and system testing, to ensure that the software is of high quality and meets the needs of its users.

Top comments (0)