DocTest++ – Extracting test cases embedded in Source Files

UnitTest++ is brilliant in how it lets you structure and manage your tests cases but it still requires a “focus-switch”.  As I am writing a function, I should be writing a test for it, but going to another file (or compilation unit for the technical term) makes me loose focus of where I am and what I was doing.  Clearly I am only mortal and this doesnt apply to the uber coder gods out there.  And this is where python’s doctest is awesome.  Then and there test insertion (yes they are validated at a different phase but atleast you can see what tests are around).  Not to mention more info on the documentation.  After all tests cases as documentation are a pretty powerful idea.

So it hit me.  Why not doctests for C++.  Lo behold DocTest++.  Wrote this last weekend because out of laziness I was’nt really adding test cases to my mango code, as much I should.  Mango certainly does have a few unit tests (and growing rapidly) and even more certainly has no plans to replace those unit tests.  But why not doctests as well?  So DocTest++ extracts tests embedded with C/C++ code and creates test cases for them.  More at the project page on git hub for how it works and what it does.

And to go a step further, this is part of the build process in my make file. A target XXX.test.cpp is (automatically) added for file XXX that contains the @test tag.  This gets compiled along with all tests.  More importantly if any of the files change or new @test tags are added, these are picked by the build system.  Wooot!

Any improvements or feedback?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s