使用外部依赖项运行 VC++ 单元测试时"Failed to set up execution context"
"Failed to set up execution context" when running VC++ unit test with external dependencies
我有一个解决方案(在此链接的 Git 上可用(,包括一个项目(生成 DLL 库(和一个本机单元测试。
- Visual Studio Enterprise 2015 with VC++
- 在视窗 10 上
我的解决方案的结构如下:
./src
+--DelaunayTriangulator.UnitTest
| |--DelaunayTriangulatorTest.cpp
| |--DelaunayTriangulator.UnitTest.vcxproj
+--DelaunayTriangulator
| |--DelaunayTriangulator.cpp
| |--DelaunayTriangulator.h
| |--DelaunayTriangulator.vcxproj
|--Triangulator.sln
该项目
我的源项目工作和构建良好。它链接了一些库(AFAIK,它们基本上是静态库(,这些库只是我需要作为依赖项的一些 CGAL 内容。它运行良好。
如果您看一下该项目,您会发现我将这些.lib
文件链接为链接器选项的一部分:
<Link>
<AdditionalDependencies>$(CGALDirPath)buildlibDebugCGAL-vc140-mt-gd-4.12.lib;$(CGALDirPath)auxiliarygmpliblibgmp-10.lib;$(CGALDirPath)auxiliarygmpliblibmpfr-4.lib;..</AdditionalDependencies>
...
</Link>
测试项目
单元测试项目是使用 Visual Studio 中的本机测试项目演练和模板创建的。测试项目还链接了与源项目相同的.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
文件之前,项目确实构建了,但根本没有运行,显示以下错误消息:
消息:无法设置执行上下文以运行测试
错误
一旦我添加了.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没有复制源工件。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- 使用外部依赖项运行 VC++ 单元测试时"Failed to set up execution context"
- SDL_Init失败并显示'SDL_Error: Failed to connect to the Mir Server'
- libgit2:git_clone() 失败并出现"failed to create ssl object"错误
- 尝试在 Kdevelop中调试时如何修复"WebConnect: Starting server failed. Try to remove stale socket."?
- QCamera::start给出神秘的"failed to start"日志消息
- openCL 和"Failed to set kernel argument"失败在哪里出错?
- Eclipse debug: "Error in final sequence - Failed to execute MI command"
- "QFileSystemWatcher: failed to add paths"但仍然有效
- 错误:'Failed to specialize function template' C2893 'std::invoke'
- C# DLL 导入失败:"The application has failed to start because its side-by-side configuration is incorrect
- MFC 错误仅在发布模式下"Failed to create an empty document"
- GLFW "WGL: Failed to find a suitable pixel format"
- Visual Studio 2015 Update 1 C++ "The breakpoint failed to bind"
- GLFW 无法创建窗口:"GLX: Failed to create context: GLXBadFBConfig"