views:

164

answers:

5

Using a small (currently at 150 loc, probably less than 500 when finished) C project I'm working on, I'm teaching myself test driven development. Based on some stuff I've found on the web - especially these slides by Olve Maudal, I've just been using asserts in my unit tests.

Since I'm just learning tdd, I have thus far avoided the overhead of also learning a unit testing framework such as cunit. At this point, my thinking is that the additional learning curve - even if shallow - of a framework is not worth the effort for such a small project. I'm wondering: Is this incorrect (i.e. I'd be better off learning the framework now)? At what point does learning a framework pay off?

+2  A: 

Hi there.

From my experience with learning .NET unit testing frameworks, I pretty much started with a small project which then gave me the advantage of being able apply what I'd learned to other projects later on, regardless of their size.

I guess you might be thinking what's the point, it's only a small bit of code, right now, but I think you would be doing well to get into a framework now rather then later. It's a bit like driving a car - I learned to drive when I was 17 years old, though I didn't get my own car until two years later. I could have just ignored taking lessons until I had got my car, but I passed and already had the skills to just go on and drive.

Bit of a weird analogy, but it's how I think when it comes to learning something like a framework, or library - you just never know what it will come in handy for you.

Cheers. Jas.

Jason Evans
+2  A: 

Learn an xUnit framework - when you get onto programming larger scale commercial projects you'll find they are used extensively.

Paolo
Thanks for the response. Most of my work is in scientific environments, and tends toward relatively small projects. However, I am leaning toward an xunit framework, especially since I'm likely to use others as I switch languages.
GreenMatt
+1  A: 

The break-even for learning a unit test framework is not long to reach. Two main advantages one gets with a framework over using assert() function are

  • more detailed message for assertion failure: expected vs. actual value
  • execution of all test cases in lieu of stopping on first failed assertion

The two of those give hints as to where the error is located.

philippe
A: 

Since I'm just learning tdd, I have thus far avoided the overhead of also learning a unit testing framework such as cunit. At this point, my thinking is that the additional learning curve - even if shallow - of a framework is not worth the effort for such a small project. I'm wondering: Is this incorrect (i.e. I'd be better off learning the framework now)? At what point does learning a framework pay off?

I agree with philippe, Jason and Paolo, but wanted to throw in yet another way of looking at it:

The so-called xUnit frameworks are a very simple and light, both as a library to download (binaries of e.g. latest JUnit or CUnit are around 200 kilobytes), and, more imporantly, conceptually.

If you're doing (or would like to do) any unit testing at all, you would definitely benefit from using a proven tool, and learning xUnit will start paying off right away. I think you may have overestimated the "overhead" involved somewhat – don't let the word "framework" scare you ;-) – you'll be up and running with xUnit in half an hour or less.

Disclaimer: Your question isn't tagged C, so I took it as language-agnostic one. I don't know if unit testing is very different on C side (compared to Java), but I doubt it. :-)

Jonik
A: 

One note: if you are following the normal pattern of red-green-refactor, then as you refactor your unit tests, the resultant code should evolve towards being a unit test framework. For instance, you shouldn't have two copies of code that does

if (expected != actual) {printf("%d != %d", actual, expected); return -1;}

That should be refactored into something like

void AssertNotEqual(int expected, int actual, char* message)
{
    if (expected != actual) {printf(message, actual, expected); return -1;}
}
John Saunders