user655261
user655261

Reputation: 99

Unit Testing legacy C++ Code with CPPUnit

I am tasked with managing a large code base written in vc++ 6.0, I need to start building unit test for portions of the code. I have set up CPPUnit and it works with my projects DLL's the problem I am facing is as follows. The legacy application is made up of 10 static libraries and one huge executable MFC application that contains 99% of the code. My unit test framework is running in another project within the same workspace and will test the 10 libraries no problem all include and references are ok, when I try to do the same for the large MFC application I get a linker error as I do not have a dll for the application. Is there any way to unit test the application without putting the test code directly inside the application.

Upvotes: 1

Views: 664

Answers (5)

John Deters
John Deters

Reputation: 4391

If you can't refactor your project to move the business logic into a new static library, try linking your test project against your project's intermediate object files, which you can probably find in BigProject\debug or BigProject\debug\obj . You can't link to the .EXE as you've discovered.

This achieves the same results as the copy process that Chad suggested while avoiding the actual duplication of source code, which would be a really bad thing.

Upvotes: 0

MaximG
MaximG

Reputation: 104

To test your main application you can set up a test project which includes the source files you want to test - not sure how easy it is to achieve with VC6, do not have it at hand, but in VS2005 and later this is quite straightforward.

So in your solution you end up with a structure like this:

MyLegacySystem.sln
  MyApplication.proj
    Main.cpp
    BusinessRules.cpp
  MyApplicationUnitTests.proj
    UnitTestsMain.cpp
    BusinessRules.cpp
    BusinessRulesTests.cpp

If for whatever reason you cannot include your source files in 2 projects, you can pull the sources into your test project by invoking the preprocessor magic:

BusinessRulesStub.cpp:
#include "..\src\BusinessRules.cpp"

However, this is essentially a temporary fix. As already suggested, in the end most of the code should be extracted into separate libraries.

Upvotes: 0

quamrana
quamrana

Reputation: 39414

You should carry on as you are:

  1. You have one test application that references libraries.
  2. You have one main application that also references those libraries.

Either move code from the main application into the existing libraries, or, preferably, move code into new libraries. Then your test application can access more code without ever referring to the application.

You know when you are done when the source for the application consists of one module which defines main() and everything else in in libraries which are tested by the test application.

Upvotes: 1

Eric
Eric

Reputation: 19873

My experience with unit testing is usually the opposite. Create a project for your test then import code from your other projects.

You can't link to the MFC application probably because your functions aren't exported. They exist, but have no mean to communicate with other applications unlike DLLs.

Upvotes: 1

Chad
Chad

Reputation: 19052

I know of no way to link against an executable file. Refactoring the code by moving the business logic to a DLL and leaving the application as a "Front-end" would be the most obvious solution. However, since it is legacy code it is likely more appropriate to simply duplicate the code for purposes of unit testing. This is not ideal, and since it is an MFC applicaiton may not be trivially easy.

Upvotes: 0

Related Questions