Unit Testing in C Part 1 – Introduction

This article is the series on Unit testing in C and carries the discussion on Unit Testing and its implementation. The aim of this series is to provide easy and practical examples that anyone can understand. This is the Introduction of Unit testing – Unit testing in C tutorial Part 1.

You can also read ceedling installation, Unity, Cmock, and stringizing, token pasting in C.

Unit Testing in C – Introduction

Anyone who has been involved in the software development life cycle (SDLC) for a while, will have encountered some form of testing. Software testing is an acceptance mechanism for discovering how well a software works according to the specified requirements. Although the aim of testing is to find bugs, it cannot guarantee the absence of other faults, no matter how creative the test cases have been designed. Unit testing enables a more thorough level of acceptance testing.

What is Unit Testing?

A unit is simply a small piece of code for any single function. So when we test those units, it is called a unit test. The unit test is a short script or piece of code designed to verify the behavior of a particular unit independently to produce a pass or fail result. Unit Testing is performed during the application development phase. Unit testing is performed usually by developers. In V-model, SDLC, and STLC the unit testing is the first phase of testing before integration testing. It is a white box testing technique and QA engineers can also perform Unit Testing if required.

However, it can sometimes be quite difficult to write a good unit test for a particular piece of code. Having difficulty testing their own or someone else’s code, developers often think that their struggles are caused by a lack of some fundamental testing knowledge or secret unit testing techniques.

In this unit testing tutorial series, I intend to demonstrate that unit tests are quite easy; the real problems that complicate unit testing.

Why is Unit Testing necessary and its use?

Sometimes, developers skip out unit testing due to lack of time. Skipping out unit testing leads to more defect fixing costs during integration, Beta, and system testing. Proper unit testing at the time of application development, saves time and money. Here are the key reasons to perform Unit Testing.

  • The defects can be fixed early at the development stage, and it saves time and costs.
  • It helps developers to understand the code base and make changes quickly that you think of.
  • Good unit tests generally serve as the project documentation.
  • Unit tests can be reused and migrated to the new project quickly when required. You should tweak the code a bit to run again.
  • Better Design – When developers write unit tests, their emphasis is on thinking about how their code will be used throughout the system, which generally results in better design.

Unit Test on Embedded Software/Firmware

Unit tests can help you write a better embedded software. To allow unit testing for a software project, the R&D team must write a testable, modular code – code that can be divided into self-contained units that can be tested. On top of making the code testable, embedded software developers must make sure their code is portable. The unit test won’t test the functionality like how it is running in hardware.

Do we need hardware (target) to run the unit tests?

Not really, if you use ceedling framework for unit tests, then you don’t need your target hardware. You can use your PC to run the unit test. In our full unit test series, we are going to discuss ceedling only. So your PC machine is enough to learn.

The misconception of Unit test

  • Our firmware is very simple, we don’t need unit testing.
  • We can’t unit test microcontroller code, it’s too close to the hardware.
  • Unit testing will add an unnecessary cost burden to our project.

What is the unit testing framework?

A unit test framework is just some code/application that makes it easier to run, test the code which we have written, and recorded the results of unit tests.

Frameworks used for Unit Test

So, now we know that unit testing is a valuable activity for embedded firmware. There are many frameworks that are available for each programming language. We have listed some tools for our C and Embedded platform. You can use anyone for unit testing.

  • Ceedling
  • Embunit
  • MinUnit
  • Criterion
  • LCUT etc.

In our next tutorial, we will see the code coverage and its types.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Table of Contents