0

I have a Visual Studio solution organised like this:

ProjectA // A static library I'm working on
ProjectB // A static library containing UnitTest++ test suites for ProjectA
ProjectC // An executable test runner which links to ProjectA and ProjectB

ProjectB contains two files which look like this:

// RunTests.h

#ifndef RUNTESTS_H
#define RUNTESTS_H

#include "UnitTest++.h"

int runAllTests();

#endif

and this:

// RunTests.cpp

#include "RunTests.h"
int runAllTests()
{
   return UnitTest::RunAllTests();
}

As well as several files containing test suites e.g.:

// FooTests.cpp

#include "RunTests.h" // 
#include "Foo.h" // From ProjectA
TEST(SomeTest)
{
   CHECK(true);
}

ProjectC consists of a single file:

// main.cpp

#include "RunTests.h" // from ProjectB

int main()
{
   return runAllTests();
}

The reason I have the tests and the test runner separated, is that I have another project which uses the same tests to analyse code coverage, which I need to keep separate as it is not cross-platform, whereas the test runner is.

The issue is, that when I compile and run ProjectC, no tests are actually run (UnitTest++ runs, but with zero tests). This is because ProjectC does not reference any symbols relating to the tests from ProjectB, so the linker doesn't link the object files from ProjectB.lib.

It is my understanding that if ProjectB was an executable, I would not have this issue (presumably because the linker would link all the object files), as per the documentation:

The general idea is that you keep one Main.cpp file with the entry-point which calls RunAllTests().

Then you can simply compile and link new .cpp files at will, typically one per test suite.

Each of the Test*.cpp files will contain one or more TEST macro incantations with the associated test code. There are no source-level dependencies between Main.cpp and Test*.cpp, as the TEST macro handles the registration and setup necessary for RunAllTests() to find all tests compiled into the same final executable.

How can I resolve this problem without having to declare all the tests in header files that ProjectC can see (which would kill UnitTest++'s ease of use)? One possibility I've noticed in Visual Studio is:

Project Settings > Configuration Properties > Linker > Input > Force Symbol References

However it would be rather tedious to have to add every single symbol, every time I write a new unit test. Is there some way I can force it to include the entire contents of ProjectB.lib? Or perhaps some code-based solution?

EDIT: What I'm looking for is something like this but for Visual Studio.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72

1 Answers1

0

I was trying to use UnitTest++ the same way you've described and ran into the same problem. I ran across a suggestion in another forum that seems to work for my unittest executable (i.e. ProjectC).

For ProjectC: Project Settings > Common Properties > (select ProjectB) > Use Library Dependency Inputs: True

This worked for me. I think what this does is effectively link any object files from ProjectB into ProjectC (as opposed to linking the library).

Tom
  • 86
  • 1
  • 4