Unit Tests in Pelles C on Windows

I said before that I might try and do some unit testing in C, rather than use the features in Visual Studio. It would mean that for H2D2 I can do everything inside Pelles C and won't need to have two IDEs running. But I've never done any unit testing in C before (at least if I have it was so long ago I've forgotten). So I needed to go off and find some means of doing that.

So I found the libtap library which is a unit testing framework for C programs. Well, it is something that implements the Test Anything Protocol which you can read about here. I'm no expert on the Test Anything Protocol, this is the first time I've used it, but for my purposes it looks like it will do fine. As long as I can run code and check for expected results and have the Passes and Failures shown in some sort of output then I'll be happy... But for the moment it looks ideal. I've had to make some minor modifications to get it working in my project (in Pelles C for Windows) but it was not very much effort really. I've just made my own version of tap.c and included it in my project. I had to include an implementation of vasprintf() which I found here rather than rolling my own, but that seems to work quite well for me.

I found libtap after searching for C unit testing on stack overflow in case you're wondering how I found it in the first place.

What I've ended up with is a #define which tells the compiler which version of main() to include. One version is my usual code and the other runs my test suite. I haven't began porting my actual H2D2 unit tests back over from C# yet, but I do have a trivial example running just to check that I can run some unit tests. So this this is all I've got so far:

I've also set it up so that the content of tap.c is not included when I compile normally, so this means that my code isn't bloated with the test framework when I'm not using it.

I've resisted the temptation to mess around with libtap too much, I've only made enough changes to get it working in my project. Thanks need to go to Nik Clayton, who wrote the library and to whoever River Tarnell-7 is for posting his vasprintf() implementation. Thanks! In case it helps any other people using Pelles C who would like to add units tests to their project, here are the files:

Download libtap, adapted for Pelles C on Windows.

I'll probably try this out on the Raspberry Pi soon, because I think it may be useful to have a lightweight unit testing framework for GCC on the Raspberry Pi too. I'll post an update if anything comes of that idea.

The joy of tests

Since my H2D2 project has quite a lot of code in it now, I've been using .Net interop as a quick way to write some unit tests for the H2D2 programming language.

I have written a C# unit test project in Visual Studio which tests much of the existing H2D2 language syntax. This means that I can add new features and quickly check that I've not broken any of the existing code. So I can very easily make sure that programs which used to run continue to work as expected.

Recently, I've been making H2D2 support function calls, and so it's been handy to write tests for various different types of function call (passing parameters, reading return values, allowing functions that don't return a value, etc). The unit tests have made it much easier to ensure that everything works after I've made changes.

Since I've written more unit tests in C# than anything else, I thought it would be the quickest way to get my tests up and running. And because I had already compiled H2D2 into a windows dll (the unmanaged kind), it is not very hard to run H2D2 programs from .Net - even easier because I had already written some basic wrapper code for all the interop stuff. It's not much harder than putting some H2D2 source code into a string and calling a Run() method. Obviously all this has been done 'just for fun', since it's only one of my hobbyist projects anyway.

But when I mentioned to one of my colleagues that I had spent some of my free time writing unit tests for fun he said that I was clearly stark raving mad. There's a possibility he's correct.

However, when I think of how I'd feel if I broke some of my existing H2D2 code and had to go back and unpick where I went wrong, I reckon that these unit tests are really less about having fun and more about preventing a severe loss of fun if I ended up scratching my head and trying to figure out what I'd done to make something break.

So I hope that's a suitable explanation for why I've been writing unit tests in my free time. It's the best excuse I've got.

Although ultimately, I may end up looking for a unit testing framework for C so that I only need one IDE loaded to run my tests... The .Net interop solution may just be a stepping stone.

Mocking null implementations of Interfaces

People who know me understand that I like pretty code.  This post describes something that I came up with to make my units tests look more pretty.  Say you want to run a unit test on a service, and normally that service would log lots of stuff (using log4net for example).  But for the purpose of the unit test you just want to throw away all the log entries.  You might use moq, and do something like this:

log4net.ILog logToNull = new Mock<ILog>(MockBehavior.Loose).Object;
var service = new MyService(logToNull);
service.DoSomething();

...which is all very fine; but not especially pretty.  I decided to make it look a little better -  what if we add this class to our project:

using Moq; 

namespace MyNamespace

{

    public class NullOf<T> where T: class

    {

        public static readonly T Instance = new Mock<T>(MockBehavior.Loose).Object;

    }

}

It’s not very complicated, but means that now we can do this:

 

var service = new MyService(NullOf<log4net.ILog>.Instance);

service.DoSomething();

Ahh, we still have a “null” implementation of the ILog interface, but to me saying “NullOf<log4net.ILog>.Instance“ looks just a bit nicer :-) Better than that, it’s generic, so you can use it on other interfaces too.  Null implementations all round then.