简体   繁体   English

如何在 Visual Studio 2017/2019 中为特定目标框架运行单元测试?

[英]How do you run unit tests for a specific target framework in Visual Studio 2017/2019?

I am really loving the new .csproj format.我真的很喜欢新的.csproj格式。 It is so much better than that dreaded (limited) project.json .它比那个可怕的(有限的) project.json好得多。

However, there is one thing that I am trying to work out.但是,我正在努力解决一件事。 I have merged my (multiple) test projects into a single multi-targeted project.我已将我的(多个)测试项目合并到一个多目标项目中。

<TargetFrameworks>netcoreapp1.0;net40;net35</TargetFrameworks>

But, there doesn't seem to be any tooling in Test Explorer in Visual Studio to select the target framework - it always just runs the first one.但是,Visual Studio 的测试资源管理器中似乎没有任何工具来选择目标框架——它总是只运行第一个。 I found a workaround - to add a <TargetFramework> element with a specific framework...我找到了一种解决方法 - 添加具有特定框架的<TargetFramework>元素...

<TargetFramework>net35</TargetFramework>

But, is there any way to select the target framework without resorting to hand-editing the MSBuild ( .csproj ) file?但是,有什么方法可以选择目标框架而无需手动编辑 MSBuild ( .csproj ) 文件? I am looking for some option in the GUI to do this - specifically so I don't have to remember to edit the .csproj file in order to switch frameworks before debugging a test or to remember to have to remove this line before the release.我正在 GUI 中寻找一些选项来执行此操作 - 特别是这样我就不必记住编辑.csproj文件以便在调试测试之前切换框架或记住必须在发布之前删除此行。

I know that the question is about VS, but I find useful that when targeting multiple frameworks dotnet tests command will run tests for all frameworks in <TargetFrameworks> node:我知道这个问题是关于 VS 的,但我发现当面向多个框架时, dotnet tests命令将对<TargetFrameworks>节点中的所有框架运行测试很有用:

> dotnet test
...
Test run for [projectPath]\bin\Debug\netcoreapp1.1\XUnitTestProject.dll(.NETCoreApp,Version=v1.1)
...
Test run for [projectPath]\bin\Debug\net461\XUnitTestProject.dll(.NETFramework,Version=v4.6.1)
...

NCrunch can also recognize multiple targets and run tests for every target automatically: NCrunch还可以识别多个目标并自动为每个目标运行测试:

在此处输入图片说明

Best option currently is to change the order of your target frameworks in the csproj.当前的最佳选择是更改 csproj 中目标框架的顺序。

<PropertyGroup>
    <TargetFrameworks>netcoreapp2.1;net45;net46;net461;net462;net47</TargetFrameworks>
</PropertyGroup>

If wanting to debug unit tests for net45 framework, you'll need to change it to:如果要调试 net45 框架的单元测试,您需要将其更改为:

<PropertyGroup>
    <TargetFrameworks>net45;net46;net461;net462;net47;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>

The UI for doing this in Visual Studio would be relatively simple to implement but they have not done so as of this answer.在 Visual Studio 中执行此操作的 UI 实现起来相对简单,但截至本答案,他们还没有这样做。

It turns out that Microsoft has finally fixed this in Visual Studio 2019 (not sure exactly when).事实证明,微软终于在 Visual Studio 2019 中修复了这个问题(不确定确切时间)。

If you specify multiple target frameworks:如果您指定多个目标框架:

<TargetFrameworks>netcoreapp2.1;net451</TargetFrameworks>

The GUI now displays: GUI 现在显示:

在此处输入图片说明

and lets you run tests on all target platforms at once.并让您一次在所有目标平台上运行测试。

使用“或”运算符在运行设置文件中使用 TargetFrameWorkVersion。

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

相关问题 Xamarin(Android)单元测试在Visual Studio 2017中 - Xamarin (Android) Unit Tests in Visual Studio 2017 Visual Studio 2017目标框架问题 - Visual Studio 2017 Target Framework problems 在Visual Studio 2017中选择.NET Target Framework - Selecting .NET Target Framework in Visual Studio 2017 目标框架未在 Visual Studio 2019 中显示 - target framework not showing in visual studio 2019 如何在Visual Studio 2017中运行MSBuild包目标 - How to run MSBuild pack target in Visual Studio 2017 如何从Visual Studio 2017中的.NET Framework 4.5控制台应用程序引用.NET标准库? - How Do You Reference a .NET Standard Library from a .NET Framework 4.5 Console Application in Visual Studio 2017? Visual Studio 2017:使用启动项目运行测试 - Visual Studio 2017: Run tests with Startup Projects 没有可用于在Visual Studio 2017中运行测试的源 - no source available to run tests in Visual Studio 2017 如何在Visual Studio 2017项目(新的.csproj文件格式)中设置`OutputPath`,而不会使目标框架混乱已解析的路径? - How do I set `OutputPath` in a Visual Studio 2017 project (new .csproj file format) without the target framework cluttering the resolved path? 在Visual Studio中运行应用程序时如何运行单元测试 - How to run unit tests when running the application in visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM