tags:

views:

173

answers:

3

I mainly develop in native C++ on Windows using Visual Studio.

A lot of times, I find myself creating a new function/class or whatever, and I just want to test that piece of logic I just wrote, quickly.

A lot of times, I have to run the entire application, which sometimes could take a while since there are many connected parts.

Is there some sort of tool that will allow me to test that new piece of code quickly without having to run the whole application?

i.e.

Say I have a project with about 1000 files, and I'm adding a new class called Adder. Adder has a method Add( int, int );

I just want the IDE/tool to allow me to test just the Adder class (without me having to create a new project and write a dummy main.cpp) by allowing me to specify the value of the inputs going into Adder object. Likewise, it would be nice if it would allow me to specify the expected output from the tested object.

What would be even cooler is if the IDE/tool would then "record" these sets of inputs/expected output, and automatically create unit tester class based on them. If I added more input/output sets, it would keep building a history of input/outputs.

Or how about this: what if I started the actual application, feed some real data to it, and have the IDE/tool capture the complete inputs going into the unit being tested. That way, I can quickly restart my testing if I found some bugs in my program or I want to change its interface a bit. I think this feature would be so neat, and can help developer quickly test / modify their code.

Am I talking about mock object / unit testing that already exists?

Sidenote: it would be cool if Visual Studio debugger has a "replay" technology where user can step back to find what went wrong. Such debugger already exists here: http://www.totalviewtech.com/

A: 

I would go with Boost.Test (see tutorial here)). The idea would be to add a new configuration to your project, which would exclude from build all unnecessary cpp files. You would just have to add .cpp files to describe the tests you want to pass.

I am no expert in this area but i have used this technique in the past and it works !

Benoît
+2  A: 

It's very easy to get started with static unit testing in C++ - three lines of code.

VS is a bit poor in that you have to go through wizards to make a project to build and run the tests, so if you have a thousand classes you'd need a thousand projects. So for large projects on VS I've tended to organised the project into a few DLLs for independent building and testing rather than monolithic ones.

An alternative to static tests more similar to your 'poke and dribble' script could be done in python, using swig to bind your code to the interpreter, and python's doc tests . I haven't used both together myself. Again, you'd need a separate target to build the python binding, and another to run the tests, rather than it being just a simple 'run this class' button.

Pete Kirkham
A: 

I think you are talking about unit testing and mock objects. Here are couple of C++ mock object libraries that might be useful :-

floehopper