'testing .net4.8 in VScode using dotnet CLI

I am trying to transform an old non-SDK-style mstest project that was generated with Visual Studio into a new SDK-style project using only Visual Studio Code.

I read and followed this little how-to, but this is only for .NET core and newer; not for .NET framework.

I need my project to target both. If I do it like this

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

  <PropertyGroup>
    <TargetFrameworks>net48;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net48'">
    <PackageReference Include="Microsoft.VisualStudio.UnitTesting" Version="11.0.50727.1" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' != 'net48'">
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
    <PackageReference Include="coverlet.collector" Version="3.1.0" />
  </ItemGroup>

</Project>

and add some test like this

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyTests {

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestTwoPlusTwoIsFour()
        {
            Assert.IsTrue(2 + 2 == 4);
        }
    }

}

and run dotnet test on my csproj, then only netcoreapp3.1, net5.0, net6.0 get tested. The net48 does not occur anywhere in my console output.

What do I need to do in order to test all four frameworks?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source