Skip to content

Trim/AOT analysis warning when using parameterized tests #3312

@PhilippNaused

Description

@PhilippNaused

Repro steps:
Build this with dotnet publish --use-current-runtime -t:Rebuild:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <PublishAot>true</PublishAot>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="TUnit" Version="0.66.13" />
  </ItemGroup>
</Project>
class Tests
{
    [Test]
    [Arguments(1)]
    public async Task TestMethod(int i)
    {
        await Assert.That(i).IsEqualTo(1);
    }
}

Results in these warnings:

Trim analysis warning IL2026: [...] Using member 'TUnit.Core.Helpers.CastHelper.Cast<Int32>(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Type conversion uses reflection for custom conversions.
AOT analysis warning IL3050: [...] Using member 'TUnit.Core.Helpers.CastHelper.Cast<Int32>(Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Dynamic type conversion may require runtime code generation.

Comes from this generated code:

InvokeTypedTest = async (instance, args, cancellationToken) =>
{
    switch (args.Length)
    {
        case 1:
            await instance.TestMethod(TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]));
            break;
        default:
            throw new global::System.ArgumentException($"Expected exactly 1 argument, but got {args.Length}");
    }
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions