[英]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.