简体   繁体   English

使用 .NET Core MSTest 运行单元测试:“未找到以下 TestContainer...”

[英]Running unit tests with .NET Core MSTest: “The following TestContainer was not found…”

I've searched high and low and can't find answer to this Exception.我搜索了高低,找不到这个异常的答案。 This question is the main one to come up when I search, but it doesn't address this issue.这个问题是我搜索时出现的主要问题,但它没有解决这个问题。

[7/8/2018 6:22:22 PM Informational] Executing test method 'CoreScraper.FlyerScraper.GetAllCurrentFlyers'
[7/8/2018 6:22:22 PM Error] System.InvalidOperationException: The following TestContainer was not found 'C:\Users\Username\Documents\Visual Studio 2017\Projects\ProductApp\CoreScraper\bin\Debug\netcoreapp2.0\CoreScraper.dll'
   at Microsoft.VisualStudio.TestWindow.Controller.TestContainerProvider.<GetTestContainerAsync>d__61.MoveNext()

"CoreScraper" is the name of the project. “CoreScraper”是项目的名称。 When I look in that ...\\netcoreapp2.0\\ folder, the CoreScraper.dll is definitely in there.当我查看...\\netcoreapp2.0\\文件夹时,CoreScraper.dll 肯定在那里。 I am running the test by right-clicking in the test method and selecting "Run Tests".我通过右键单击测试方法并选择“运行测试”来运行测试。 The test ran fine the very first time I ran it, but it has given me this error ever since.我第一次运行时测试运行良好,但从那以后它给了我这个错误。 I've closed out of Visual Studio and reopened, deleted the contents of bin\\ , cleaned and rebuilt the project, etc.我已经关闭了 Visual Studio 并重新打开,删除了bin\\的内容,清理并重建了项目等。

What worked for me:什么对我有用:

Delete folders inside .vs folder.删除.vs文件夹内的文件夹。

But you still can't delete that folder ?但是你还是不能删除那个文件夹?

Probably because of Live Unit Testing , testhost.exe or testhost.x86.exe process is the cause.可能是因为Live Unit Testingtesthost.exetesthost.x86.exe进程是原因。

The problem was that the NuGet package Microsoft.NET.Test.Sdk was not installed.问题是未安装 NuGet 包Microsoft.NET.Test.Sdk Installing this package in the project via the NuGet Package Manager solved the problem.通过 NuGet 包管理器在项目中安装这个包解决了这个问题。

This wasn't intuitive to me since I have another unit test project that runs fine without the Microsoft.NET.Test.Sdk package, but that project is .NET Framework 4.6.2.这对我来说并不直观,因为我有另一个单元测试项目,它在没有Microsoft.NET.Test.Sdk包的情况下运行良好,但该项目是 .NET Framework 4.6.2。

Today I faced similar issue with Visual Studio 2017 for xUnit Tests.今天,我在 Visual Studio 2017 for xUnit Tests 中遇到了类似的问题。 My Observations:我的观察:

  1. Issue arise for me when switching between Release/Debug mode.在发布/调试模式之间切换时出现问题。
  2. Solution : Restart Visual Studio and it resolved the issue.解决方案:重新启动 Visual Studio 即可解决问题。

Note After some Google/Bing, I discovered everyone got the solution by installing some packages.注意在谷歌/必应之后,我发现每个人都通过安装一些软件包得到了解决方案。 However in my case everything was working before and hence decided not to install anything additional.但是,就我而言,之前一切正常,因此决定不安装任何其他内容。

Hope it helps.希望能帮助到你。

As @Marathon55 said installing Microsoft.NET.Test.Sdk solved the issue but in my case i've to install 2 more packages MSTest.TestFramework & MSTest.TestAdapter .正如@Marathon55 所说,安装Microsoft.NET.Test.Sdk解决了这个问题,但就我而言,我必须再安装 2 个软件包MSTest.TestFrameworkMSTest.TestAdapter might help someone可能会帮助某人

I'm Running VS2019, Version 16.7.2.我正在运行 VS2019,版本 16.7.2。 I resolved my problem by deleting the hidden TestStore folder ('.vs[solution name]\\v16\\TestStore').我通过删除隐藏的 TestStore 文件夹('.vs[solution name]\\v16\\TestStore')解决了我的问题。 Seems that a unittest project that exists in another branch is cached in that TestStore folder when switching branch.似乎在切换分支时存在于另一个分支中的 unittest 项目缓存在该 TestStore 文件夹中。

This is what has happened in my case.这就是我的情况。

  1. My Visual Studio solution has 9 projects我的 Visual Studio 解决方案有 9 个项目
  2. I deleted one test project from solution but still remains on disk我从解决方案中删除了一个测试项目,但仍保留在磁盘上
  3. Delete above test project from disk从磁盘删除上面的测试项目
  4. Visual Studio's Test Explorer still shows tests from the deleted project, but unable to run them (hence showing the error that OP mentioned) Visual Studio 的测试资源管理器仍然显示已删除项目中的测试,但无法运行它们(因此显示了 OP 提到的错误)
  5. Deleting .vs folder ultimately removed all the references of old project.删除.vs文件夹最终删除了旧项目的所有引用。 Now the Test Explorer shows expected number of tests.现在测试资源管理器显示预期的测试数量。

I had this problem when I added my new test project as a "Class Library" project instead of a "Unit Test Project".当我将新测试项目添加为“类库”项目而不是“单元测试项目”时,我遇到了这个问题。 It found my tests but couldn't run them.它找到了我的测试,但无法运行它们。

Fixed it by deleting the class library project and starting over with the new Unit Test Project.通过删除类库项目并从新的单元测试项目重新开始修复它。

I had this issue.我有这个问题。 And I tried every answer that is posted here in this question.我尝试了在此问题中发布的所有答案。 In my instance, what was wrong - someone modified app.config file incorrectly.在我的例子中,出了什么问题 - 有人错误地修改了app.config文件。 As result, the file gained a new configuration section in it but without definition of this section.结果,该文件在其中获得了一个新的配置部分,但没有定义该部分。

Missing <configSections></configSections>缺少<configSections></configSections>

While having <someConfigSection . . . . />虽然有<someConfigSection . . . . /> <someConfigSection . . . . />

I simply fixed app.config and test started to work.我只是修复了app.config并测试开始工作。 This is the source of the original suggestion that app.config can be the culprit of this issue 这是最初建议app.config可能是此问题的罪魁祸首的来源

For me the problem resolved when i switched back from release mode to debug mode.对我来说,当我从发布模式切换回调试模式时问题解决了。

(While using VS2019) (使用VS2019时)

I don't know if this helps anyone, but I encountered this issue with a Specflow project that I was trying to target .net Standard.我不知道这是否对任何人有帮助,但是我在尝试针对 .net Standard 的 Specflow 项目中遇到了这个问题。 In the end the only way to fix this problem was to target .net Core instead.最后,解决此问题的唯一方法是改为针对 .net Core。

I am using .NET Framework v4.6.1, not .NET Core, but I also had trouble with this exact same error.我使用的是 .NET Framework v4.6.1,而不是 .NET Core,但我也遇到了这个完全相同的错误。 I followed a combination of the steps provided by the guys / gals up above.我按照上面的人/女孩提供的步骤进行了组合。

  • Install the MSTest.TestAdapter NuGet package安装 MSTest.TestAdapter NuGet 包
  • Install the MSTest.TestFramework NuGet package安装 MSTest.TestFramework NuGet 包
  • Install the Microsoft.NET.Test.Sdk NuGet package (may not be required?? unsure)安装 Microsoft.NET.Test.Sdk NuGet 包(可能不需要??不确定)
  • close all running instances of VS 2019 and delete the vs/ folder关闭所有正在运行的 VS 2019 实例并删除 vs/ 文件夹
  • reopen VS 2019重新打开 VS 2019
  • rebuild the test project重建测试项目

...and then it just worked. ...然后它就起作用了。 Hope this helps someone!希望这可以帮助某人!

I have had the same problem with a .NET Core project.我在 .NET Core 项目中遇到了同样的问题。 Here is how to solve it:以下是解决方法:

  1. create empty folder (any name you want)创建空文件夹(任何你想要的名字)
  2. run command " dotnet new mstest ".运行命令“ dotnet new mstest ”。 This command creates a unit-test project, with all necessary nuget packages!此命令创建一个单元测试项目,其中包含所有必需的 nuget 包! Then you can examine the.csproj file, here it is:然后你可以检查 .csproj 文件,这里是:

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <RootNamespace>my_project_unittest</RootNamespace> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"> <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" /> <PackageReference Include="MSTest.TestFramework" Version="2.1.0" /> <PackageReference Include="coverlet.collector" Version="1.2.0" /> </ItemGroup> </Project>

In my case I was messing with the test project and at some point decided to change the config from:在我的情况下,我弄乱了测试项目,并在某些时候决定从以下位置更改配置:

<Project Sdk="Microsoft.NET.Sdk">

to

<Project Sdk="Microsoft.NET.Sdk.Web">

but later undid the change.. :)但后来取消了更改.. :)

Then I faced the same issue as everyone here.. After doing countless rebuilds, disabling resharper, cleaning caches, folders, downgrading packages, removal of packages, etc.. - I decided to create a new test project and compare the configuration (surprisingly new project's tests worked in debugging mode)!然后我遇到了和这里所有人一样的问题..在做了无数次重建、禁用 resharper、清理缓存、文件夹、降级包、删除包等之后。 - 我决定创建一个新的测试项目并比较配置(令人惊讶的是新的项目的测试在调试模式下工作)!

To make a long story short - check for the Properties folder.. there should be NO such folder for the Test project!长话短说 - 检查Properties文件夹.. Test 项目应该没有这样的文件夹! The folder was probably created when I changed the SDK version earlier.该文件夹可能是我之前更改 SDK 版本时创建的。

After removal of this folder and project rebuild I was finally able to debug the test!删除此文件夹并重建项目后,我终于能够调试测试了! No other solution worked.没有其他解决方案有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM