Kjara
Kjara

Reputation: 2912

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?

Upvotes: 5

Views: 3396

Answers (2)

riQQ
riQQ

Reputation: 12863

It works for me using the dotnet command from the .NET SDK 8.0.403 and a test project with a newer MSTest version. It seems like they added support for this at some point.

Note: you can also reference the NuGet meta package MSTest instead of the individual packages.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="MSTest">
      <Version>3.6.1</Version>
    </PackageReference>
  </ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="MSTest.TestAdapter">
      <Version>3.6.1</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestFramework">
      <Version>3.6.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk">
      <Version>17.11.1</Version>
    </PackageReference>
  </ItemGroup>
</Project>

Upvotes: 1

Kjara
Kjara

Reputation: 2912

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

Upvotes: 3

Related Questions