testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Provide minimal working project for using MTP

Open cbersch opened this issue 3 months ago • 8 comments

Hi,

this may seem a silly question, but I'm actually struggling to create a minimal project with .NET10-RC2, which uses MTP.

Using the examples from https://devblogs.microsoft.com/dotnet/mtp-adoption-frameworks/ simply doesn't work, I get an MSBuild error about unknown switch --project.

Then I tried to create a minimal project: Tests.csproj:

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

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>

    <EnableMSTestRunner>true</EnableMSTestRunner>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MSTest.TestFramework" Version="4.0.1" />
    <PackageReference Include="Microsoft.Testing.Platform" Version="2.0.1" />
  </ItemGroup>
</Project>

global.json:

{
  "test": {
    "runner": "Microsoft.Testing.Platform"
  }
}

Tests.cs:

[TestClass]
public sealed class CalculatorTests
{
    [TestMethod]
    public void Add_WhenCalled_ReturnsSum() => Assert.AreEqual(3, Calculator.Add(1, 2));

    private static class Calculator
    {
        public static int Add(int a, int b) => a + b;
    }
}

When running this with dotnet test --project Tests/Tests.csproj, I get the error

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Get projects properties with MSBuild didn't execute properly with exit code: 1.

What am I missing?

cbersch avatar Oct 17 '25 06:10 cbersch

@cbersch In your csproj, you referenced MSTest.TestFramework only. You need MSTest.TestAdapter for the project to be recognized as a test project and get an entrypoint generated for it.

Youssef1313 avatar Oct 17 '25 06:10 Youssef1313

@Youssef1313 Thanks for the quick answer! Yes, this was indeed missing (and explicit reference to Microsoft.Testing.Platform isn't needed).

Two things, which were unclear to me:

  • I wasn't aware of the strict necessity of using <EnableMSTestRunner>, this isn't mentioned anywhere at https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test In the end, I found the "correct" migration steps at https://learn.microsoft.com/en-us/dotnet/core/testing/migrating-vstest-microsoft-testing-platform
  • At first I was still using .NET-RC1, but having the test runner in the global.json as introduced only in RC2. I hadn't expected such a fundamental change in a later RC!

Just for completeness: the following project is the minimal one for NUnit:

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

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>

    <EnableNUnitRunner>true</EnableNUnitRunner>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NUnit" Version="4.4.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
  </ItemGroup>
</Project>

Now I only have to find out all the remaining things, like runsettings, passing runsettings parameters via commandline :)

cbersch avatar Oct 17 '25 07:10 cbersch

For NUnit/MSTest, runsettings is supported via --settings.

The article https://learn.microsoft.com/dotnet/core/testing/unit-testing-with-dotnet-test is focusing more on dotnet test rather than how to enable MTP for individual test frameworks. The EnableMSTestRunner property is documented in https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-runner-intro (similar for NUnit, documented in https://learn.microsoft.com/dotnet/core/testing/unit-testing-nunit-runner-intro).

I do think, however, that cross-linking those articles with a note should help here. The documentation feedback is very important for us to have better clarity for users. Thanks so much for providing that feedback!

Youssef1313 avatar Oct 17 '25 07:10 Youssef1313

I do think, however, that cross-linking those articles with a note should help here. The documentation feedback is very important for us to have better clarity for users. Thanks so much for providing that feedback!

Yes, that would help. Thank you

cbersch avatar Oct 17 '25 07:10 cbersch

I'll keep this issue open to track updating our documentation.

Youssef1313 avatar Oct 17 '25 08:10 Youssef1313

@Youssef1313 Sure, sorry.

cbersch avatar Oct 17 '25 08:10 cbersch

How about this minimal project?

<Project Sdk="MSTest.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>

stan-sz avatar Oct 23 '25 06:10 stan-sz

FWIW, I had to create two minimal projects to report a bug.

  • With Microsoft.NET.Sdk: https://github.com/carl-quintero/Sample-Microsoft.NET.Sdk
  • With MSTest.SDK: https://github.com/carl-quintero/Sample-MSTest.SDK

They work if you remove the Directory.Build.props file (which is present in the repos to show a bug to MS in another ticket https://github.com/issues/created?issue=microsoft%7Ctestfx%7C6955)

carl-quintero avatar Nov 19 '25 15:11 carl-quintero