.net 为什么 Visual Studio 2015/2017/2019 Test Runner 没有发现我的 xUnit v2 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35103781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:45:41  来源:igfitidea点击:

Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests

.netvisual-studiounit-testingvisual-studio-2015xunit2

提问by Ruben Bartelink

UPDATE: Adding a 2019; the discovery/runner integration mechanism is same as per 2017 & 2015, so the key things that can go wrong are the same.

更新:添加 2019 年;发现/运行器集成机制与 2017 年和 2015 年相同,因此可能出错的关键事情是相同的。



I've read Why is the xUnit runner not finding my tests, which covers reasons xUnit would neverbe able to find your testsbut my problem is different - I'm confident there's nothing subtle going on with my tests; (they have worked in other environments, this seems to be just my machine) - the Visual Studio Test Runner in Visual Studio 2015 [Community Edition] is simply not showing anyof my tests. I'm not doing anything remotely exciting; the tests target xUnit.net v2 on the Desktop.

我已经阅读了为什么 xUnit runner 找不到我的测试,其中涵盖了 xUnit永远无法找到您的测试的原因,但我的问题有所不同 - 我相信我的测试没有任何微妙之处;(他们在其他环境中工作过,这似乎只是我的机器) - Visual Studio 2015 [社区版] 中的 Visual Studio 测试运行程序根本没有显示我的任何测试。我没有做任何令人兴奋的事情;测试针对桌面上的 xUnit.net v2。

I've looked in the Output window and am not seeing anything in at all under Testin the Show output fromtabs.

我查看了“输出”窗口,但在“选项卡的显示输出”中的测试”下根本看不到任何内容。

回答by Ruben Bartelink

  1. Eliminate discovery exceptions from your inquiries; go to the output Window (Ctrl-Alt-O), then switch the show output fromdropdown (Shift-Alt-S) to Testsand make sure there are no discovery exceptions

  2. Test|Test settings|Default processor architecturecan help if your tests are x86/x64 specific and discovery is triggering bittedness-related exceptions, i.e. not AnyCpu

  3. As suggested in this answer(upvote it if the technique helps)running the desktop console runner (instructions) can be a good cross check to eliminate other possibilities, e.g. mangled config files:-

    packages\xunit.runner.console.2.2.0\tools\xunit.console <tests.dll>

    NOTE The xunit.runner.consolepackage is deprecated - when you get stuff working in VS, you'll be able to have dotnet testrun them in CI contexts too

  1. 从您的查询中消除发现异常;转到输出窗口(Ctrl-Alt-O),然后将显示输出从下拉列表(Shift-Alt-S)切换到测试并确保没有发现异常

  2. 测试|测试设置|如果您的测试是特定于 x86/x64 的并且发现正在触发与比特性相关的异常,即不是AnyCpu,则默认处理器架构会有所帮助

  3. 正如此答案中所建议的(如果技术有帮助,请对其进行投票)运行桌面控制台运行程序(说明)可以是一个很好的交叉检查以消除其他可能性,例如损坏的配置文件:-

    packages\xunit.runner.console.2.2.0\tools\xunit.console <tests.dll>

    注意该xunit.runner.console包已被弃用 - 当您在 VS 中dotnet test运行一些东西时,您也可以在 CI 上下文中运行它们



Go read the documentation- it's comprehensive, up to date, includes troubleshooting info and takes PRs:-

去阅读文档- 它是全面的、最新的,包括故障排除信息并需要 PR:-

Important note: If you've previously installed the xUnit.net Visual Studio Runner VSIX (Extension), you must uninstall it first. The Visual Studio runner is only distributed via NuGet now. To remove it, to go Tools> Extensions and Updates. Scroll to the bottom of the list, and if xUnit.net is installed, uninstall it. This will force you to restart Visual Studio.

If you're having problems discovering or running tests, you may be a victim of a corrupted runner cache inside Visual Studio. To clear this cache, shut down all instances of Visual Studio, then delete the folder %TEMP%\VisualStudioTestExplorerExtensions. Also make sure your project is only linked against a single version of the Visual Studio runner NuGet package (xunit.runner.visualstudio).

重要说明:如果您之前安装了 xUnit.net Visual Studio Runner VSIX (Extension),则必须先将其卸载。Visual Studio 运行器现在仅通过 NuGet 分发。要删除它,请转到Tools> Extensions and Updates。滚动到列表底部,如果安装了 xUnit.net,请将其卸载。这将强制您重新启动 Visual Studio。

如果您在发现或运行测试时遇到问题,您可能是 Visual Studio 中运行程序缓存损坏的受害者。要清除此缓存,请关闭 Visual Studio 的所有实例,然后删除文件夹%TEMP%\VisualStudioTestExplorerExtensions. 还要确保您的项目仅链接到单个版本的 Visual Studio 运行程序 NuGet 包 ( xunit.runner.visualstudio)。

The following steps worked for me:

以下步骤对我有用:

  1. (Only if you suspect there is a serious mess on your machine - in general the more common case is that the visual studio integration is simply not installed yet)

    Do the DEL %TEMP%\VisualStudioTestExplorerExtensionsas advised :-

    PS> del $env:TEMP\VisualStudioTestExplorerExtensions

  2. Install the NuGet Package xunit.runner.visualstudioin all test projects

    • Paket:

      .paket\paket add nuget xunit.runner.visualstudio -i
      

      You need to end up with the following in your paket.dependencies:

      nuget xunit.runner.visualstudio version_in_path: true

      Note the version_in_path: truebit is important

    • Nuget: Go to Package Manager Console (Alt-T,N,O) and

      Install-Package xunit.runner.visualstudio)
      

    Rebuild to make sure xunit.runnerends up in the output dir

  3. Close Test Explorer <- this was the missing bit for me

  4. Re-open Test Explorer (Alt-S,W,T)

  5. Run All tests (Ctrl R, A)

  1. (仅当您怀疑您的机器出现严重混乱时 - 通常更常见的情况是尚未安装 Visual Studio 集成)

    执行DEL %TEMP%\VisualStudioTestExplorerExtensions的建议: -

    PS> del $env:TEMP\VisualStudioTestExplorerExtensions

  2. xunit.runner.visualstudio在所有测试项目中安装 NuGet 包

    • 包:

      .paket\paket add nuget xunit.runner.visualstudio -i
      

      您需要在您的paket.dependencies.

      nuget xunit.runner.visualstudio version_in_path: true

      注意version_in_path: true位很重要

    • Nuget:转到包管理器控制台 (Alt-T,N,O) 和

      Install-Package xunit.runner.visualstudio)
      

    重建以确保xunit.runner最终出现在输出目录中

  3. 关闭测试资源管理器<- 这对我来说是缺失的一点

  4. 重新打开测试资源管理器(Alt-S、W、T)

  5. 运行所有测试 (Ctrl R, A)

回答by Max

I had to change the Test Settings after changing the test projects CPU to x64. Then the tests where detected again.

将测试项目 CPU 更改为 x64 后,我不得不更改测试设置。然后再次检测到测试。

Architecture

建筑学

回答by Arman

None of the above solutions worked for me (dotnetcore 1.1, VS2017). Here's what fixed it:

以上解决方案都不适合我(dotnetcore 1.1,VS2017)。这是修复它的内容:

  1. Add NuGet Package Microsoft.TestPlatform.TestHost
  2. Add NuGet Package Microsoft.NET.Test.Sdk
  1. 添加 NuGet 包 Microsoft.TestPlatform.TestHost
  2. 添加 NuGet 包 Microsoft.NET.Test.Sdk

Those are in addition tothese packages I installed prior:

这些是我之前安装的这些软件包的补充

  • xunit (2.3.0-beta1-build3642)
  • xunit.runner.visualstudio (2.3.0-beta1-build1309)
  • xunit (2.3.0-beta1-build3642)
  • xunit.runner.visualstudio (2.3.0-beta1-build1309)

回答by Chris Aelbrecht

Install xunit.runner.visualstudiopackage for the test project

xunit.runner.visualstudio为测试项目安装包

回答by Venkat Ramanan

Follow this steps :

请按照以下步骤操作:

  1. Update your MsTest.TestAdapterand MsTest.TestFrameworkdll'sfrom nugget package manager.
  2. Clean your solution
  3. Build your solution.
  1. 更新您的MsTest.TestAdapterMsTest.TestFrameworkdll'snugget package manager.
  2. 清洁您的解决方案
  3. 构建您的解决方案。

回答by Tom Makin

I've been struggling with this all afternoon whilst working with an ASP Core project and xUnit 2.2.0. The solution for me was adding a reference to Microsoft.DotNet.InternalAbstractions

在处理 ASP Core 项目和 xUnit 2.2.0 时,我整个下午都在为此苦苦挣扎。我的解决方案是添加对Microsoft.DotNet.InternalAbstractions

I found this out when trying to run the test project manually with dotnet testwhich failed but reported that InternalAbstractionswas missing. I did not see this error in the test output window when the auto discovery failed. The only info I saw in the discovery window was a return code, which didn't mean anything to me at the time, but in hindsight was probably indicating an error.

我在尝试手动运行测试项目时发现了这一点,该项目dotnet test失败但报告InternalAbstractions缺少。自动发现失败时,我没有在测试输出窗口中看到此错误。我在发现窗口中看到的唯一信息是返回码,当时这对我来说没有任何意义,但事后看来可能表明存在错误。

回答by Liam

It's happened to me a couple of times - when I Clean the project and Build it again it tends to be fine.

它发生在我身上几次 - 当我清理项目并再次构建它时,它往往没问题。

回答by Jawad Sabir

The reason in my case was the target build was not the same between project debugger and test runner. To unify those elements:

在我的情况下,原因是项目调试器和测试运行器之间的目标构建不同。要统一这些元素:

  1. Test>Test Settings>Default Processor Architecture. then select either X64 or X86.
  2. Project>(your project)Properties>Build(tab)>platform target.
  1. 测试>测试设置>默认处理器架构。然后选择 X64 或 X86。
  2. 项目>(您的项目)属性>构建(选项卡)>平台目标。

After they are identical, rebuild your solution then test methods will appear for you.

在它们相同后,重建您的解决方案,然后将为您显示测试方法。

回答by shaeed

Make sure that your test class is public.

确保您的测试类是public

回答by SohamC

I am using xUnit 2.2.0.

我正在使用 xUnit 2.2.0。

My issue was my solution was not able to find certain dlls and app.configwas trying to resolve them. The error was not showing up in the test output window in Visual Studio.

我的问题是我的解决方案无法找到某些 dll 并app.config试图解决它们。该错误未显示在 Visual Studio 的测试输出窗口中。

I was able to identify the error when I installed xunit.runner.consoleand tried to run the tests through command line.

当我安装xunit.runner.console并尝试通过命令行运行测试时,我能够识别错误。

How to run xunit tests in CLI.

如何在 CLI 中运行 xunit 测试