Skip to content

Unable to swap TestingPlatformExplorer sample to NUnit #7088

@joshcomley

Description

@joshcomley

I have attempted to adapt the sample, but when I run, it doesn't detect any tests.

If I remove Program.cs, it will run and detect the tests.

With Program.cs, I get the following:

Image

I have made the changes outlined below to the TestingPlatformExplorer sample project.

It feels like this should just work?

TestingPlatformExplorer.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RootNamespace>TestingPlatformExplorer</RootNamespace>
    <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
    <EnableNUnitRunner>true</EnableNUnitRunner>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NUnit" Version="3.14.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
    <!-- Update to the last version if needed -->
    <PackageReference Include="Microsoft.Testing.Platform" Version="1.9.1" />

    <!-- Needed to support the Trx capability -->
    <PackageReference Include="Microsoft.Testing.Extensions.TrxReport.Abstractions" Version="1.9.1" />

<!--    &lt;!&ndash; Add the trx extension &ndash;&gt;-->
    <PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.9.1" />

    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.0.4" />

    <PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="1.9.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
  </ItemGroup>

  <ItemGroup>
    <None Update="TestingPlatformExplorer.testingplatformconfig.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <ProjectCapability Include="DiagnoseCapabilities" />
    <ProjectCapability Include="TestingPlatformServer" />
    <ProjectCapability Include="TestContainer" />
  </ItemGroup>

</Project>

Program.cs

using System.Reflection;

using Microsoft.Testing.Platform.Builder;

using TestingPlatformExplorer.TestingFramework;

var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
// Register the testing framework
testApplicationBuilder.AddTestingFramework(() => new[] { Assembly.GetExecutingAssembly() });
using var testApplication = await testApplicationBuilder.BuildAsync();
return await testApplication.RunAsync();

UnitTests.cs

using NUnit.Framework;

using TestingPlatformExplorer.TestingFramework;

using Assert = NUnit.Framework.Assert;

namespace TestingPlatformExplorer.UnitTests;

[TestFixture]
public class SomeTests
{
    [Test]
    public static void TestMethod1() => Assert.AreEqual(1, 1);

    [Test]
    public static void TestMethod2() => Assert.AreEqual(1, 2);

    [Test]
    public static void TestMethod3(ITestOutputHelper testOutputHelper)
    {
        testOutputHelper.WriteLine("I'm running TestMethod3");

        try
        {
            int a = 1;
            int b = 0;
            int c = a / b;

            Assert.AreEqual(c, 2);
        }
        catch (Exception ex)
        {
            testOutputHelper.WriteErrorLine(ex.ToString());
            throw;
        }
    }

    [Ignore("Temporary disabled")]
    [Test]
    public static void TestMethod4() => Assert.AreEqual(1, 1);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions