使用 Visual Studio 构建工具构建和 运行 单元测试

Building and running unit tests with Visual Studio build tools

我正在使用 Google 测试适配器向现有 C++ Visual Studio 项目添加单元测试。

在 Visual Studio 2019 的计算机上 运行 一切正常,但是当我尝试在构建服务器上 运行 它们时,出现以下错误

error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets.

但是,我们没有使用 NuGet 进行包管理。我尝试安装它,但抱怨缺少文件夹。这不是 .NET 项目,所以我认为这是一个转移注意力的问题。

我能够使用 Visual Studio 安装程序在我的计算机上安装 Google 测试适配器,但它没有显示为构建服务器上 VS 构建工具的一部分。

运行 msbuild -t:restore 没有帮助,它只是报告“无事可做”。

我不明白为什么 Google 测试适配器不适用于 VS 构建工具,因为它似乎是构建单元测试所必需的。有谁知道为什么它不起作用?处理此问题的最佳做法是什么?

谢谢!

问题是你的c++项目遗漏了googletest nuget包的内容。所以解决方案是在你的 c++ 项目中恢复整个 nuget 包。

更新 1

首先,备份一个全新的项目,将其恢复到问题开始时的状态。

外,msbuild -t:restore命令适用于PackageReference nuget management format.

的项目

因为你的c++项目使用packages.config nuget management format, msbuild -t:restore will not work. See this official document而不是,你应该使用nuget restore命令。

此命令适用于您当前的项目,运行使用此命令将恢复 nuget 包,然后您将永远不会遇到这个问题。

在使用它之前,您应该下载nuget.exe CLI并将其路径配置到系统环境变量PATH中,以便CMD 可以调用 nuget.

配置步骤nuget.exe,可参考

步数

1)删除解决方案文件夹下的packages文件夹

2)然后,打开构建工具,运行:

nuget restore xxx\xxx\xxx.sln(the full path of solution file containing the c++ project and the unit test project)

然后,就可以用命令构建工程了。我希望错误会消失。