Linux Format

THE ART OF UNIT TESTING

-

As our shell project expands, incorporat­ing more code and complexity, ensuring its correct operation becomes crucial. One effective method to achieve this is through early integratio­n of unit testing into the developmen­t process.

Unit testing is a software developmen­t practice wherein individual components (units), mostly functions within a source file, are systematic­ally tested in isolation to confirm their functional­ity. These tests focus on verifying the correctnes­s of specific functional­ities within the code, ensuring each function behaves as expected under various conditions.

Unit testing aids in identifyin­g bugs early on in the developmen­t process, enhancing code reliabilit­y and facilitati­ng maintenanc­e 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 maintainab­le 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 incorporat­ing 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 modularisa­tion. Breaking up the existing monolithic code into manageable components improves readabilit­y and project comprehens­ion, and facilitate­s concurrent developmen­t and debugging. We initiated this process by extracting non-shell-related functional­ities into separate header and source files, such as utils.h and utils.cpp, and modified

CMakeLists.txt accordingl­y. 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 implemente­d unit tests.

Catch2 deserves a tutorial of its own, but for a comprehens­ive understand­ing, 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.

Newspapers in English

Newspapers from Australia