This content originally appeared on DEV Community and was authored by Oleksandra
To add unit tests into my CLI tool I used Catch2, a modern C++ unit testing framework that makes writing tests easy and quick. It has a nice single-header option that you can drop into a repo, simple REQUIRE / CHECK macros, and flexible test naming and tagging. Also, Catch2 was the library I found interesting when I was looking at open-source projects during the first week of OSD600 cource, so I already liked it.
To write unit tests, I first created a tests/ folder in my project and added the Catch2 single-header file there. Then I wrote separate test files for different parts of my project, such as test_utils.cpp, test_file_reader_scanner.cpp, and test_compressor.cpp. Each test file included catch.hpp and my project headers, and I used TEST_CASE blocks with REQUIRE or CHECK statements to verify expected behavior. I compiled the tests together with the project source files to produce a test executable. Running this executable ran all the test cases, and I could filter by tags or test names to run specific tests.
For this assignment, I also tried to complete the optional tasks, such as Test Runner Improvements and Code Coverage Analysis, but I got stuck. For the test runner, using a file watcher like watchexec initially caused an infinite loop because my test binary was inside a watched directory, so every rebuild triggered another rebuild. I thought moving the binary to a separate build/ folder would fix it, but it didn’t completely solve the issue. For code coverage, I tried using GCC’s gcov and lcov, but generating a proper coverage report was tricky. The .gcno files are created at compile time, and the .gcda files are created only when the test executable runs. I ran into issues with missing files and paths, and I wasn’t able to fix them the way I wanted.
Overall, this assignment gave me my first experience writing unit tests for C++ code. I had never done testing like this before, and it was nice to work with Catch2. Even though I didn’t fully complete the optional tasks, I think I will definitely explore these topics more and use that in my future C++ projects.
This content originally appeared on DEV Community and was authored by Oleksandra
Oleksandra | Sciencx (2025-11-07T01:21:23+00:00) Adding Unit Tests to repo-context-packager. Retrieved from https://www.scien.cx/2025/11/07/adding-unit-tests-to-repo-context-packager/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.