【问题标题】:"Failed to set up execution context" when running VC++ unit test with external dependencies运行具有外部依赖项的 VC++ 单元测试时“无法设置执行上下文”
【发布时间】: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;
    }
};

我知道这是由于外部参考的某种问题。这里有什么问题?

【问题讨论】:

标签: c++ visual-studio unit-testing visual-c++


【解决方案1】:

所以这里的问题对我的配置来说有点特殊,但也足够笼统,值得其他可能遇到这种情况的开发人员回答。

问题是我的源项目的.dlls 没有部署到测试输出文件夹。所以你需要在你的测试项目属性中设置OutDir

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  <LinkIncremental>true</LinkIncremental>
  <OutDir>$(ProjectDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>

这将使测试实际复制的dll不在解决方案文件夹中,而是在测试项目文件夹中,然后引用的源项目dll将被正确复制。测试项目文件没有 OutDir 的条目,似乎使 MSBuild 没有复制源工件。

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多