【发布时间】:2018-07-07 19:19:53
【问题描述】:
我有一个解决方案(可在 Git 上this link 获得),包括一个项目(生成 DLL 库)和本机单元测试。
- 带有 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>
测试项目
已使用 Visual Studio 中的 native test project 演练和模板创建了单元测试项目。 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 文件之前,项目确实构建但根本没有运行,显示以下错误消息:
消息:无法设置执行上下文来运行测试
错误
只要我添加了.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;
}
};
我知道这是由于外部参考的某种问题。这里有什么问题?
【问题讨论】:
-
相关(相同的错误信息,可能原因不同):Unit Testing issue in Visual Studio 2012
标签: c++ visual-studio unit-testing visual-c++