The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Unit tests for legacy C++ code

Hi,

I am trying to incorporate unit testing into my large legacy C++ code base on Unix/Linux platform. I am using CxxTest.

When I got a new project, I created unit tests for the new methods/functions that I added to the existing class (say 'Controller').

The problem is that the 'Controller' class includes many other header files. I cannot create a 'testrunner' binary that links just Controller.o and unit_test.o as I get many undefined symbols during linking.

Is there any automatic way of 'stubbing' out the undefined methods (calls to other classes) at link time or is there any other way?

Regards
Rajesh
Rajesh Radhakrishnan Send private email
Wednesday, February 06, 2008
 
 
It sounds like you're using Unix, so I'll go with that assumption.

What I've done is to change how my legacy C++ code links.  Basically, I compile everything except the file with main in it, and create a library, say mylib.a.  Then my existing legacy code links main.o and mylib.a together.

My unit tests link to mylib.a directly.
Michael Gibson Send private email
Wednesday, February 06, 2008
 
 
Pick up a copy of "Working effectivley with Legacy code" it describes a lot of these problems.
Martin Send private email
Wednesday, February 06, 2008
 
 
Hi Martin,

I read the book on "Working with Legacy code". The book seems to be more catered towards the Java/Web world. The main thing I could get from the book was that a serious amount of refactoring is required before unit tests can be written. This is not possible for a large C++ legacy code base in a reasonable amount of time. I am looking for a solution that meets half way, like stubbing out the dependencies, shells/wrappers around functions that are not going to be unit tested.

Regards
Rajesh
Rajesh Radhakrishnan Send private email
Wednesday, February 06, 2008
 
 
Maybe you could do manual unit tests in a new class and link everything together in your same program.

Then add code so that if you type program.exe /runalltests, your main() function invokes UnitTest::RunAllTests().

Manually creating unit tests is a bit tedious but, well, if it's your only option...
Daniel Daranas Send private email
Thursday, February 07, 2008
 
 
> The problem is that the 'Controller' class includes many other header files.

The link path is under your influence when you build, right? So when you build your unit test executable you just need something satisfying the dependencies, such as a mock object - http://mockpp.sourceforge.net/

Thursday, February 07, 2008
 
 
You could build your unit tests into the main program. I'm doing this for my latest project, I just call the program with a command line option and it runs the tests. This will solve your linking problems.
Tony Edgecombe Send private email
Thursday, February 07, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz