我有一个解决方案(可在 Git 上获取 this link ),其中包括一个项目(生成 DLL 库)和一个 native 单元测试。
- 带有 VC++ 的 Visual Studio Enterprise 2015
- 在 Windows 10 上
我的解决方案的结构如下:
./src
+--DelaunayTriangulator.UnitTest
| |--DelaunayTriangulatorTest.cpp
| |--DelaunayTriangulator.UnitTest.vcxproj
+--DelaunayTriangulator
| |--DelaunayTriangulator.cpp
| |--DelaunayTriangulator.h
| |--DelaunayTriangulator.vcxproj
|--Triangulator.sln
项目
我的源项目运行良好并且构建良好。它链接了一些库(AFAIK,它们基本上是静态库),这些库只是一些 CGAL我需要作为依赖项的东西。它也运行良好。
如果您查看project ,您会发现我将这些 .lib
文件链接为链接器选项的一部分:
<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>
测试项目
单元测试项目已使用native test project创建Visual Studio 中的演练和模板。 test project还链接与源项目相同的 .lib
文件。以下是我的单个测试:
#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
在我从 CGAL 链接这些 .lib
文件之前,该项目确实构建了,但根本没有运行,显示了以下错误消息:
Message: Failed to set up the execution context to run the test
错误
一旦我添加了 .lib
文件,该项目就会构建,并且仅当我未注释 Assert
行时才会运行单个单元测试(我必须评论引用我的源项目的所有代码):
TEST_CLASS(DelaunayTriangulatorTest) {
public:
TEST_METHOD(PerformTriangulation) {
Assert::AreEqual<int>(0, 0, L"Wrong result", LINE_INFO());
}
};
当我取消注释引用我的项目的代码(使用我在源项目中定义的类)时,当我尝试运行测试时会显示相同的错误消息:
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;
}
};
据我所知,这是由于外部引用的某种问题造成的。这里有什么问题吗?
最佳答案
因此,这里的问题对我的配置来说有点特殊,但也足够通用,值得为可能遇到这种情况的其他开发人员提供答案。
问题是我的源项目的 .dll
未部署到测试输出文件夹。因此,您需要在测试项目属性中设置 OutDir
:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
这将使测试实际上复制的dll不是在解决方案文件夹中,而是在测试项目文件夹中,然后引用的源项目dll将被正确复制。测试项目文件没有 OutDir
条目,这似乎使得 MSBuild 无法复制源工件。
关于c++ - 使用外部依赖项运行 VC++ 单元测试时为 "Failed to set up execution context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51226286/