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.
- 1 Unit Testing in C – Introduction
- 2 What is Unit Testing?
- 3 Why is Unit Testing necessary and its use?
- 4 Unit Test on Embedded Software/Firmware
- 5 Do we need hardware (target) to run the unit tests?
- 6 The misconception of Unit test
- 7 What is the unit testing framework?
- 8 Frameworks used for Unit Test
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.
- LCUT etc.
In our next tutorial, we will see the code coverage and its types.
You can also read the below tutorials.