33

I am really loving the new .csproj format. It is so much better than that dreaded (limited) 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. I found a workaround - to add a <TargetFramework> element with a specific framework...

<TargetFramework>net35</TargetFramework>

But, is there any way to select the target framework without resorting to hand-editing the MSBuild (.csproj) file? 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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212

4 Answers4

5

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:

> 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:

enter image description here

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • Maybe useful to some, not useful at all for debugging - especially if you have a library and the only way to debug is to run the tests within Visual Studio. – NightOwl888 Apr 26 '17 at 13:39
  • @NightOwl888, doesn't look like VS currently have UI for that. Give a try to NCrunch - excellent tool for unit testing and it supports debugging by right-clicking on code line covered by test. – Andrii Litvinov Apr 26 '17 at 14:01
  • Thanks, but there is [no open source licensing](https://www.ncrunch.net/buy/faq#discount) for this product. We need to support anyone who is willing to join our team and the licensing of NCrunch doesn't meet that requirement. – NightOwl888 May 07 '17 at 14:57
2

Best option currently is to change the order of your target frameworks in the 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:

<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.

Michael Brown
  • 1,585
  • 1
  • 22
  • 36
  • This seems even worse than using `TargetFramework` to do this. Either way, I am stuck hand editing the project file(s) instead of just selecting the tests to run from the UI and on the build server there are MSBuild configuration elements that must be overridden. But if you create a VSIX extension to do this in Visual Studio, I will definitely download it. – NightOwl888 Jul 04 '19 at 07:21
2

It turns out that Microsoft has finally fixed this in Visual Studio 2019 (not sure exactly when).

If you specify multiple target frameworks:

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

The GUI now displays:

enter image description here

and lets you run tests on all target platforms at once.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
0

Use TargetFrameWorkVersion in a runsettings file using the 'or' operator.

JWP
  • 6,672
  • 3
  • 50
  • 74