THE ART OF UNIT TESTING
As our shell project expands, incorporating more code and complexity, ensuring its correct operation becomes crucial. One effective method to achieve this is through early integration of unit testing into the development process.
Unit testing is a software development practice wherein individual components (units), mostly functions within a source file, are systematically tested in isolation to confirm their functionality. These tests focus on verifying the correctness of specific functionalities within the code, ensuring each function behaves as expected under various conditions.
Unit testing aids in identifying bugs early on in the development process, enhancing code reliability and facilitating maintenance by serving as a safety net for changes. By isolating and testing individual units, developers can swiftly detect and rectify errors, resulting in more robust and maintainable software.
We selected Catch2 as the unit testing framework due to its simplicity and ease of use. Developed by Phil Nash, it stands out for its design, robust features, and user-friendly learning curve. It allows developers to write tests in a natural and readable manner using C macros that resemble plain English.
The process of incorporating Catch2 into our project involves adding it to the CMakeLists.txt file and creating a test file (such as test.cpp) where unit tests are defined. The tests are then added to the build system using CMake. With these steps completed, running make test executes the tests, providing a clear overview of their success or failure.
To introduce proper unit testing, there was a slight need for code modularisation. Breaking up the existing monolithic code into manageable components improves readability and project comprehension, and facilitates concurrent development and debugging. We initiated this process by extracting non-shell-related functionalities into separate header and source files, such as utils.h and utils.cpp, and modified
CMakeLists.txt accordingly. We have added a new target in the project, and now, issuing the magic ./lxf-shell-tests command from the build/tests directory runs all the implemented unit tests.
Catch2 deserves a tutorial of its own, but for a comprehensive understanding, you are advised to visit https://github. com/catchorg/Catch2, and in order to explore further unit tests related to the project, please visit its GitHub repo at
https://github.com/fritzone/lxf-shell.