我有一个解决方案(在此链接的 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没有复制源工件。