.netunit-testingvisual-studio-codemstestdotnet-cli

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?


Solution

  • Seems I cannot use dotnet test for it but need msbuild. (Thanks for the comment, @Heinzi.)

    I can run it like so:

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\mstest.exe" /testcontainer:"$(MSBuildProjectDirectory)\$(OutputPath)$(ProjectName).dll"
    

    It would be nice if this can somehow be incorporated into my dotnet test call, so I don't need two calls...