【发布时间】:2023-04-10 21:21:01
【问题描述】:
Visual Studio 2019 根本没有检测或发现 NUUnit 或 MSTest 单元测试。一周前我刚刚安装了它。此处的 MS 指南 https://docs.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>
我尝试过的线程:Tests not running in Test ExplorerWhy will Visual Studio 2019 will not run my unit tests?Visual Studio 2019 Test Explorer puts all tests under "Not Run Tests" 如果我创建一个面向 .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 模块,将它们添加回来,然后它对他有用,但他发回给我的项目不起作用。模块版本不变
我在 Microsoft 开发人员 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:\Users\zapho\src\c#\tutorials\working\ConsoleApp1\ConsoleApp1\NUnit.Tests2\bin\Debug\netcoreapp3.1\].
If this is a framework-dependent application, install the runtime in the default location [C:\Program Files\dotnet] 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:\Users\zapho\src\c#\tutorials\working\ConsoleApp1\ConsoleApp1\NUnit.Tests2\bin\Debug\netcoreapp3.1\].
If this is a framework-dependent application, install the runtime in the default location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.
. Please check the diagnostic logs for more information.
【问题讨论】:
-
您标记了 NUnit 和 MsTest,尽管您链接的页面和您拥有的引用都与 MsTest 有关。根据您打算使用 MsTest、NUnit 还是 xUnit,您需要做的事情略有不同,所以请把问题集中在您想使用什么上。
-
谢谢。我确实更喜欢 NUnit - 但由于我还不熟悉 CORE,如果 NUnit 实际上与 CORE 不兼容,我很乐意使用 MsTest。我最初尝试设置 NUnit,我最后一次使用 NUnit 肯定是在 6 年前。在 Windows 上回退到 .NET 4 是最不受欢迎的。
-
只是我们中的一些人(以及我)只回答我们所知道的主题的问题。例如,我回答有关 NUnit 的问题,但跳过有关 MsTest 的问题。 NUnit 在 .NET Core 上运行良好,但您必须将您的问题和示例代码集中在您尝试过但对我或其他任何人都不起作用的内容上。您可能有足够的信息来获得与 mstest 相关的答案,所以我希望有人能给您。
-
这对查理很有帮助,这意味着我应该继续努力。所以最新的 NUnit 应该与最新的 Core 一起使用,我将尝试建立一个新项目并使用包版本更明确地捕获步骤,或者只是将其推送到 github,很确定有人最终会告诉我我哪里出错了。至少如果我的第三次或第四次尝试成功,我可以回答我自己的问题。
-
您可能正在使用 VS 2019 16.9+,因此必须先升级此项目中的 NuGet 包引用,然后才能开始工作。
标签: visual-studio unit-testing nunit