I have a solution (available on Git at this link) including one project (producing a DLL library) and a native unit test.
The structure of my solution is the following:
./src
+--DelaunayTriangulator.UnitTest
| |--DelaunayTriangulatorTest.cpp
| |--DelaunayTriangulator.UnitTest.vcxproj
+--DelaunayTriangulator
| |--DelaunayTriangulator.cpp
| |--DelaunayTriangulator.h
| |--DelaunayTriangulator.vcxproj
|--Triangulator.sln
My source project works and builds fine. It links some libs (AFAIK, they are basically static libraries) which are just some CGAL stuff I need as dependencies. It also runs fine.
If you have a look at the project, you will find that I link those .lib
files as part of Linker options:
<Link>
<AdditionalDependencies>$(CGALDirPath)\build\lib\Debug\CGAL-vc140-mt-gd-4.12.lib;$(CGALDirPath)\auxiliary\gmp\lib\libgmp-10.lib;$(CGALDirPath)\auxiliary\gmp\lib\libmpfr-4.lib;..</AdditionalDependencies>
...
</Link>
The unit test project has been created by using the native test project walkthrough and template in Visual Studio. The test project is also linking the same .lib
files that the source project does. Following is the single test I have:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../DelaunayTriangulator/DelaunayTriangulator.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace CodeAlive::Triangulation;
namespace TriangulatorUnitTest {
TEST_CLASS(DelaunayTriangulatorTest) {
public:
TEST_METHOD(PerformTriangulation) {
DelaunayTriangulator* triangulator = new DelaunayTriangulator();
int result = triangulator->Perform();
Assert::AreEqual<int>(0, result, L"Wrong result", LINE_INFO());
delete triangulator;
}
}; // class
} // ns
Before I linked those .lib
files from CGAL, the project did build but did NOT run at all, showing this error message:
Message: Failed to set up the execution context to run the test
As soon as I added the .lib
files, the project did build and the single unit test did run only if I left the Assert
line uncommented (I had to comment all the code referencing my source project):
TEST_CLASS(DelaunayTriangulatorTest) {
public:
TEST_METHOD(PerformTriangulation) {
Assert::AreEqual<int>(0, 0, L"Wrong result", LINE_INFO());
}
};
When I uncomment the code referencing my project (using the classes I define in my source project), then the same error message shows up when I try running the test:
TEST_CLASS(DelaunayTriangulatorTest) {
public:
TEST_METHOD(PerformTriangulation) {
DelaunayTriangulator* triangulator = new DelaunayTriangulator();
int result = triangulator->Perform();
Assert::AreEqual<int>(0, result, L"Wrong result", LINE_INFO());
delete triangulator;
}
};
I understand that this is due to some kind of issue with external references. What is wrong here?
So the issue here was kinda peculiar to my configuration but also generic enough to deserve an answer for other devs who might incur in this situation.
The problem was that the .dll
s of my source project were not deployed to the test output folder. So you need to set OutDir
in your test project properties:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
This will make the test actually copy the dlls not in the solution folder, but in the test project folder, and then the referenced source project dlls will be correctly copied. The test project file did not have an entry for OutDir
, it seems that made MSBuild not copy the source artifacts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With