为什么 Visual Studio 2019 不会运行我的单元测试? [英] Why will Visual Studio 2019 will not run my unit tests?

查看:635
本文介绍了为什么 Visual Studio 2019 不会运行我的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VS2019 中看到 NUnit 测试的一些非常奇怪的行为,其中相同的解决方案在 VS2017 中运行良好.我的灵魂中有几个 NUnit 测试项目.

I am seeing some very strange behavior for NUnit tests in VS2019, where the same solution works fine in VS2017. There are several NUnit test projects in my soultion.

在安装了 NUnit Runner 扩展的 VS2017 中,我可以在测试资源管理器"窗口中看到我的所有测试,并且全部运行"按钮将起作用并运行所有测试.我组织中的一些开发人员使用 Resharper 而不是 NUnit 扩展,这也有效.

In VS2017 with the NUnit Runner extension installed, I can see all of my tests in the Test Explorer window and the "run all" button will work and run all tests. Some developers in my organization use Resharper instead of the NUnit extension and this works too.

我已经停止使用 Resharper,因为随着 VS 引入更多功能,Resharper 使它变得如此缓慢,以至于 VS 无法使用.

I have stopped using Resharper because as VS introduces more features, Resharper has made it so slow that VS is unusable.

在 VS2019 中,测试资源管理器窗口将显示我的所有单元测试(即使没有安装 NUnit 扩展).如果我单击全部运行",它将不会运行任何测试,并且输出"窗口将显示发现 0 个测试.同事们表示,Resharper 将毫无问题地运行所有测试.如果我右键单击单个测试项目并仅运行这些测试,则某些项目将运行测试,但不是全部.

In VS2019 the Test Explorer window will show all of my unit tests (even without the NUnit extension installed). If I click "run all" it will not run any tests and the Output window will say 0 tests discovered. Coworkers have said that Resharper will run all tests without issue. If I right click a single test project and run just those tests, some projects will run tests, but not all.

对于某些项目,我尝试安装 NUnit3TestAdapater nuget 包,如果仅选择了该项目,这将使 VS2019 运行该项目的测试.这不适用于所有项目,它仍然不适用于全部运行".

For some projects I have tried installing the NUnit3TestAdapater nuget package and this will let VS2019 run that project's test if only that project is selected. This does not work for all projects and it still does not work for "run all".

有谁知道是什么导致了这个问题,有什么办法可以解决这个问题?我已经为所有这些项目更新到最新版本的 NUnit (3.12) 和最新的 TestAdapter (3.16).

Does anyone know what might be causing this and what could fix it? I have updated to the latest version of NUnit (3.12) and latest TestAdapter (3.16) for all of these projects.

大约一年以来,这真的很痛苦,因为我需要同时安装 VS2017 和 2019,而且我需要培训新开发人员如何解决这个奇怪的问题.

This has been a real pain for about a year, because I need to keep VS2017 and 2019 both installed, and I need to train new developers on how to work around this weird issue.

推荐答案

结果证明解决方案是两件事的结合.

The solution turned out to be a combination of two things.

  1. 在顶部菜单上,导航到 Test > Options 并禁用从 C# 和 Visual Basic .NET 源文件实时发现测试".此选项似乎与参数化测试不兼容.问题是参数化测试在源代码中并不存在",而是由测试适配器在运行时生成.这也解决了我看到的一个问题,参数化测试的基本"测试在测试资源管理器中显示为未运行的测试,即使只有特定的测试用例是真正的测试.(在我看来,默认情况下不应启用此功能,因为参数化测试非常有用,而无需编译即可在测试资源管理器中查看新测试非常方便,因为无论如何您都必须编译才能运行它们.)

  1. On the top menu, navigating to Test > Options and disabling "Discover tests in real time from C# and Visual Basic .NET source files". This option appears to be incompatible with parameterized tests. The problem is that parameterized tests do not "exist" in source code but are generated by the test adapter at runtime. This also fixed an issue I've seen where the "base" test of the parameterized test shows up in the Test Explorer as a not-run test, even though only the specific test cases are real tests. (In my opinion, this should not be enabled by default because parameterized tests are extremely useful, whereas seeing new tests in the Test Explorer without compiling is a trivial convenience since you must compile to run them anyway.)

将 NuGet 包用于测试适配器,而不是使用 VSIX 扩展.似乎对于这一部分,所需要的只是您的解决方案中至少有一个项目引用了这一点.如果至少有一个项目引用它,则所有测试项目都可以运行.(这对我来说很有意义,因为它与 Visual Studio 之外的构建工具更兼容.)

Using the NuGet package for the test adapter instead of using the VSIX extension. It seems that for this piece, all that is required is that at least one project in your solution references this. If at least one project references it, all test projects can be run. (This makes sense to me, as it is more compatible with build tools outside of Visual Studio.)

突破性的改变万岁!

这篇关于为什么 Visual Studio 2019 不会运行我的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆