Visual Studio新项目不会运行测试



Visual Studio 2019根本没有检测或发现NUNit或MSTest单元测试。我一周前刚刚安装了它。这里的MS指南https://learn.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019年;tabs=mstest不起作用。

有几个线程包含了可能的解决方案,但我尝试过的都没有帮助。我是C#的新手,所以很多步骤都花了很长时间才弄清楚,但尽管我的应用程序进展顺利,但我真的很想以TDD风格工作和学习。即使创建了一个新的空白MSTest项目,解决方案中根本没有应用程序代码或库,示例/模板项目也不起作用,所以我遗漏了一些重要的东西。(我安装了.NET Core SDK——我的目标是在未来瞄准macOS和linux。(

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>
</Project>

我尝试过的线程:测试没有在测试资源管理器中运行为什么Visual Studio 2019不会运行我的单元测试?Visual Studio 2019测试资源管理器将所有测试置于";未运行测试";如果我创建了一个以.NET 4.7为目标的项目,那一切都很好,如果这有帮助的话,那就是当我想以Core为目标时,我就摆脱了困境。

再次陷入困境我不得不安装一个名为";NUnit 3";,https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTemplatesforVisualStudio工作了一会儿。。。但当我添加一个测试用例时,它又崩溃了。

nuget软件包更新了吗,仍然不起作用。

我克隆了。从git这个项目https://github.com/dotnet/samples/tree/master/core/getting-started/unit-testing-using-nunit它也不起作用。

然后,我重新安装了2019,并通过电子邮件将我的项目发送给了一位朋友,他只是删除了nunit nuget模块,将它们添加回来,然后它对他起了作用,但他发给我的项目没有起作用。模块版本不变

我在微软开发者VM/iso镜像中打开了同一个项目,该项目运行得很好。所以,我的环境在某种程度上与nunit不兼容。有办法看到一些痕迹吗?

将安装驱动器从D:更改为C:我现在收到这个错误

Testhost process exited with error: A fatal error occurred, the required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:Userszaphosrcc#tutorialsworkingConsoleApp1ConsoleApp1NUnit.Tests2binDebugnetcoreapp3.1].
If this is a framework-dependent application, install the runtime in the default location [C:Program Filesdotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.
. Please check the diagnostic logs for more information.
Testhost process exited with error: A fatal error occurred, the required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:Userszaphosrcc#tutorialsworkingConsoleApp1ConsoleApp1NUnit.Tests2binDebugnetcoreapp3.1].
If this is a framework-dependent application, install the runtime in the default location [C:Program Filesdotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.
. Please check the diagnostic logs for more information.

您需要将Microsoft.NET.Test.SdkNuGet包添加到解决方案中,以便在Visual Studio中运行测试。

修改您的项目文件,

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
</ItemGroup>
</Project>

通过最终设置环境变量修复了它

DOTNET_ROOT=D:Program Filesdotnet

根据答案https://stackoverflow.com/a/61453119/337598

最新更新