Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"The test framework adapter has not been registered" for simple Testing.Platform example #4267

Closed
edyoung opened this issue Dec 7, 2024 · 3 comments
Assignees

Comments

@edyoung
Copy link
Member

edyoung commented Dec 7, 2024

I am trying out the Microsoft.Testing.Platform functionality. From the Note in the overview docs, it sounds like for simple cases I should not need to explicitly create a Main() function or do any setup in it. Everything compiles OK, but when I run the test executable I get

Unhandled exception. System.InvalidOperationException: The test framework adapter has not been registered. Use 'ITestApplicationBuilder.RegisterTestFramework' to register it
   at Microsoft.Testing.Platform.Builder.TestApplicationBuilder.BuildAsync() in /_/src/Platform/Microsoft.Testing.Platform/Builder/TestApplicationBuilder.cs:line 102
   at TestingPlatformEntryPoint.Main(String[] args) in C:\onebranch\plattest\plattest\obj\Debug\net9.0\TestPlatformEntryPoint.cs:line 14
   at TestingPlatformEntryPoint.<Main>(String[] args)

I think there is something I need to add to my .csproj but I'm not clear what it is. It might be helpful to have a sample project that doesn't do any explicit setup.

Plattest.csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

 <ItemGroup>
   <PackageReference Include="Microsoft.Testing.Platform.MSBuild" />
   <PackageReference Include="MSTest.TestFramework" />
 </ItemGroup>

</Project>

Directory.Packages.props:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>    
    <PackageVersion Include="Microsoft.Testing.Platform" Version="1.4.3" />
    <PackageVersion Include="Microsoft.Testing.Platform.MSBuild" Version="1.4.3" />    
    <PackageVersion Include="MSTest.TestFramework" Version="3.6.4" />    
  </ItemGroup>
</Project>

Tests.cs:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Tests
{
    [TestMethod]
    public void Test1()
    {
        Assert.AreEqual(1, 1);
    }

    [TestMethod]
    public void Test2()
    {
        Assert.AreEqual(2, 2);
    }
}
@Evangelink
Copy link
Member

Please have a look here https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-runner-intro#enable-mstest-runner-in-an-mstest-project. You are missing the MSTest.TestAdapter project and also the EnableMSTestRunner property:

Plattest.csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableMSTestRunner>true</EnableMSTestRunner>
  </PropertyGroup>

 <ItemGroup>
   <PackageReference Include="MSTest" />
 </ItemGroup>

</Project>

Directory.Packages.props:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>    
    <PackageVersion Include="MSTest" Version="3.6.4" />    
  </ItemGroup>
</Project>

@edyoung
Copy link
Member Author

edyoung commented Dec 7, 2024

Thank you! That works. I think I didn't notice that because it was in the MSTest rather than the Testing Platform part of the content hierarchy.

One follow up question: is this mechanism compatible with AOT? If I add <PublishAot>true</PublishAot> to my .csproj and run dotnet publish I get a number of warnings about reflection and the published .exe doesn't work.

I'm partly interested in this because we have a lot of code we would like to move to compile as AOT. I would like to unit test that everything works as expected in that mode.

Thanks again.

@Evangelink
Copy link
Member

For native aot, you will need to switch the nuget packages to use, see here https://github.com/microsoft/testfx/tree/main/samples%2Fpublic%2Fmstest-runner%2FNativeAotRunner.

Note that at the moment, we have only really partial support (test methods and parametrized tests but no fixture or other attribute and feature). If you could +1 on the issue for native aot support that would help us with convincing management on prioritizing this work.

Note 2, I'd strongly recommend the usage of MSTest.Sdk https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-sdk as it would simplify your project setup and would automatically switch the packages based on PublishAot being true or false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants