I wanted to talk a little about my approach to unit testing, and the small framework I’ve written to help me write and run tests. I am not an e

A simple C unit test framework

submited by
Style Pass
2024-10-21 10:30:06

I wanted to talk a little about my approach to unit testing, and the small framework I’ve written to help me write and run tests.

I am not an expert in unit testing. I don’t even give it that much priority, to be honest. I mean, sure, I recognize its usefulness in many circumstances, but I do feel that often when I come across unit test advocates, they can be way more passionate about tests than I personally feel is reasonable. Admittedly, my problem domain and my projects are relatively tiny – so my solution reflects that.

So, my approach and attitude is light and pragmatic. Get the most obvious benefits from unit tests, for the scenarios where they make the most sense only, and just not worry about the rest. If you feel more strongly about testing than I do, this is not the right solution for you – but luckily there are a lot of unit test solutions to choose from, most which does a lot more than mine.

The code for my framework, testfw.h, is written in C (but can be compiled in C++ too), and comes in the form of a stb-style single-file header-only library (this is not at all the same thing as a C++ template-based header library, worth noting). Single file libraries like these are my favorite way of packaging and reusing code, and I have structured all of my hobby development around them.  There is not a lot of code, only 440 lines – but then again, it also doesn’t do very much, as we will see as we go on.

Leave a Comment