Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Failed to set up execution context" when running VC++ unit test with external dependencies

I have a solution (available on Git at this link) including one project (producing a DLL library) and a native unit test.

  • Visual Studio Enterprise 2015 with VC++
  • On Windows 10

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

The project

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 test project

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

The error

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?

like image 399
Andry Avatar asked Jul 07 '18 19:07

Andry


1 Answers

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 .dlls 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.

like image 174
Andry Avatar answered Nov 08 '22 22:11

Andry