Unit Testing

Published Jan 22, 2022Updated Oct 26, 2022
Contribute to Docs

Unit testing is an important part of coding and software development because it verifies whether individual parts of the source code behave and work as expected.

These parts, or units, of code can be anything from a class function to a snippet of code that performs a specific task. Testing these units individually allows for finding errors earlier on in the development process.

Types of Unit Testing

Unit testing is usually categorized into two types:

  • Manual testing
  • Automated testing

Manual Testing

Manual testing is when tests are done directly by testers. This can involve actions such as clicking on objects, providing input, or scrolling through items.

Depending on the length of the software, this type of testing can be time-consuming. An alternative to this would be automated testing.

Automated Testing

Automated testing is when programs are written specifically to test code. This is optimal when manual testing becomes tedious, frequent testing is needed, or when multiple individuals are adding or making changes to the code.

Writing scripts to test parts of the source code is a way to automate testing. Combined with a testing automation tool, these scripts can run automatically and immediately inform developers if any errors have occurred.

Pros and Cons

Unit testing is an important part of the development phase when writing code because it can potentially highlight errors earlier in development. It’s best practice to test parts or units of code to verify its correctness during development or after making changes.

Every project can benefit from unit testing because:

  • It can reduce the chances of having compound errors.

  • Makes debugging easier.

  • Helps creators write better code by encouraging them to write in smaller chunks.

  • Makes it easier to make changes to the source code.

However, it is important to note that there are some disadvantages to unit testing including:

  • One line of code may need multiple lines of test code to test it.

  • Can’t test everything; not every execution path can be covered with unit testing.

  • It can not catch integration errors or broad system errors.

Unit-testing may seem like a time-consuming process that could take a while to get a handle on. Nevertheless, having a process in place that contributes to the robustness and correctness of the source code can save time in the long-run.

All contributors

Looking to contribute?

Learn More on Codecademy