Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 457
NightOwl888 Avatar asked Apr 22 '17 00:04

NightOwl888


People also ask

How do I run a specific unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

How do I perform a unit test in Visual Studio 2017?

In the new project dialog box, find the unit test project to use. Type test in the search box to find a unit test project template for the test framework you want to use, such as MSTest (C#) or the Native Unit Test project (C++), and select it. Starting in Visual Studio 2017 version 14.8, the .


2 Answers

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

like image 65
Andrii Litvinov Avatar answered Sep 22 '22 19:09

Andrii Litvinov


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.

like image 41
Michael Brown Avatar answered Sep 21 '22 19:09

Michael Brown